40👍
✅
The auto-reloader process turned out to be the culprit; turns out the autoreload process gets the same arguments, and goes through the same initialization process, as the original. The solution was to have the pre-server code execute only if it’s not running in the process spawned by the autoreloader, which can be detected through an environment variable:
import os
from django.contrib.staticfiles.management.commands.runserver import Command as RunserverCommand
class Command(RunserverCommand):
def run(self, *args, **options):
if os.environ.get('RUN_MAIN') != 'true':
self.stdout.write('About to start running on ' + self.addr)
super(Command, self).run(*args, **options)
28👍
The local development server runs a separate process for the auto-reloader. You can turn off the auto-reload process by passing the –noreload flag.
python manage.py runserver --noreload
- [Django]-How to test "render to template" functions in django? (TDD)
- [Django]-Can I make an admin field not required in Django without creating a form?
- [Django]-Order by count of a ForeignKey field?
Source:stackexchange.com