61π
Write middleware that does this in __init__
and afterwards raise django.core.exceptions.MiddlewareNotUsed
from the __init__
, django will remove it for all requests :). __init__
is called at startup by the way, not at the first request, so it wonβt block your first user.
There is talk about adding a startup signal, but that wonβt be available soon (a major problem for example is when this signal should be sent)
Related Ticket: https://code.djangoproject.com/ticket/13024
Update: Django 1.7 includes support for this. (Documentation, as linked by the ticket)
11π
In Django 1.7+ if you want to run a startup code and,
1. Avoid running it in migrate, makemigrations, shell sessions, β¦
2. Avoid running it twice or more
A solution would be:
file: myapp/apps.py
from django.apps import AppConfig
def startup():
# startup code goes here
class MyAppConfig(AppConfig):
name = 'myapp'
verbose_name = "My Application"
def ready(self):
import os
if os.environ.get('RUN_MAIN'):
startup()
file: myapp/__init__.py
default_app_config = 'myapp.apps.MyAppConfig'
This post is using suggestions from @Pykler and @bdoering
- [Django]-H14 error in heroku β "no web processes running"
- [Django]-Good ways to sort a queryset? β Django
- [Django]-ImportError: No module named django.core.wsgi Apache + VirtualEnv + AWS + WSGI
4π
If you were using Apache/mod_wsgi for both, use the WSGI script file described in:
http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html
Add what you need after language translations are activated.
Thus:
import sys
sys.path.insert(0, '/usr/local/django/mysite')
import settings
import django.core.management
django.core.management.setup_environ(settings)
utility = django.core.management.ManagementUtility()
command = utility.fetch_command('runserver')
command.validate()
import django.conf
import django.utils
django.utils.translation.activate(django.conf.settings.LANGUAGE_CODE)
# Your line here.
django.core.management.call_command('syncdb', interactive=False)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
- [Django]-How do I use django rest framework to send a file in response?
- [Django]-Django simple_tag and setting context variables
- [Django]-How to check the TEMPLATE_DEBUG flag in a django template?
3π
You can create a custom command and write your code in the handle function. details here https://docs.djangoproject.com/en/dev/howto/custom-management-commands/
Then you can create a startup script that runs the django server then executes your new custom command.
- [Django]-Django β How to specify which field a validation fails on?
- [Django]-How to use subquery in django?
- [Django]-Radio buttons in django Forms
- [Django]-Stack trace from manage.py runserver not appearing
- [Django]-How to completely dump the data for Django-CMS
- [Django]-What is pip install -q -e . for in this Travis-CI build tutorial?
0π
Here is how I work around the missing startup signal for Django:
https://github.com/lsaffre/djangosite/blob/master/djangosite/models.py
The code that is being called there is specific to my djangosite project, but the trick to get it called by writing a special app (based on an idea by Ross McFarland) should work for other environments.
Luc
- [Django]-How to force migrations to a DB if some tables already exist in Django?
- [Django]-Django 2 β How to register a user using email confirmation and CBVs?
- [Django]-Matching query does not exist Error in Django