Execute code when Django starts ONCE only?

前端 未结 7 1392
终归单人心
终归单人心 2020-11-22 13:55

I\'m writing a Django Middleware class that I want to execute only once at startup, to initialise some other arbritary code. I\'ve followed the very nice solution posted by

7条回答
  •  长发绾君心
    2020-11-22 14:38

    Update from Pykler's answer below: Django 1.7 now has a hook for this


    Don't do it this way.

    You don't want "middleware" for a one-time startup thing.

    You want to execute code in the top-level urls.py. That module is imported and executed once.

    urls.py

    from django.confs.urls.defaults import *
    from my_app import one_time_startup
    
    urlpatterns = ...
    
    one_time_startup()
    

提交回复
热议问题