6

I want to initialize some variables (from the database) when Django starts.

I am able to get the data from the database but the problem is how should I call the initialize method . And this should be only called once.

Tried looking in other Pages, but couldn't find an answer to it.

The code currently looks something like this ::


def get_latest_dbx(request, ....):

#get the data from database


def get_latest_x(request):

get_latest_dbx(request,x,...)


def startup(request):

get_latest_x(request)

Himanshu
  • 65
  • 1
  • 5

2 Answers2

6

Some people suggest( Execute code when Django starts ONCE only? ) call that initialization in the top-level urls.py(which looks unusual, for urls.py is supposed to handle url pattern). There is another workaround by writing a middleware: Where to put Django startup code? But I believe most of people are waiting for the ticket to be solved.

UPDATE:

Since the OP has updated the question, it seems the middleware way may be better, for he actually needs a request object in startup. All startup codes could be put in a custom middleware's process_request method, where request object is available in the first argument. After these startup codes execute, some flag may be set to avoid rerunning them later(raising MiddlewareNotUsed exception only works in __init__, which doesn't receive a request argument).

BTW, OP's requirement looks a bit weird. On one hand, he needs to initialize some variables when Django starts, on the other hand, he need request object in the initialization. But when Django starts, there may be no incoming request at all. Even if there is one, it doesn't make much sense. I guess what he actually needs may be doing some initialization for each session or user.

Community
  • 1
  • 1
Hui Zheng
  • 10,084
  • 2
  • 35
  • 40
  • I tried calling it from top level urls.py. But the function needs the request object. How do I get that in urls.py ?? – Himanshu Jan 28 '13 at 06:56
  • Actually the method which gets the list of entries from the datbase and displays them (custom written) needs the request.session, So I need to pass the request object. Will the Request object (from process_request) have the session variable assigned? – Himanshu Jan 28 '13 at 09:57
  • surely you can get session from request's property, i.e. `request.session` as long as `SessionMiddleware` is enabled. – Hui Zheng Jan 28 '13 at 10:41
  • I am not able to get the request object. SessionMiddleware is enabled. – Himanshu Jan 28 '13 at 12:42
  • Have you put 'django.contrib.sessions' in your INSTALLED_APPS and put `django.contrib.sessions.middleware.SessionMiddleware` in MIDDLEWARE_CLASSES? – Hui Zheng Jan 28 '13 at 13:01
  • How do I access the request_object? I need to create a process_request()? – Himanshu Jan 29 '13 at 06:29
  • No, request object will be available in any middleware. Once you create a middleware class, just putting you init logic in its `process_request` method. Remember, django framework(like any other framework) will feed the request object to that method, you never need create yourself, which is called IoC(Inversion of Control). – Hui Zheng Jan 29 '13 at 06:32
  • Tried a lot, still can't get it. It still says undefined global variable. can you paste a code snippet? – Himanshu Jan 30 '13 at 10:00
  • undefined global variable means you're referring a variable that has never been defined. You probably forget to add `request` in `process_request`. In middleware, it should be like `def process_request(self, request): do_sth_with(request)` – Hui Zheng Jan 30 '13 at 11:03
1

there are some cheats for this. The general solution is trying to include the initial code in some special places, so that when the server starts up, it will run those files and also run the code.

Have you ever tried to put print 'haha' in the settings.py files :) ?

Note: be aware that settings.py runs twice during start-up

Thai Tran
  • 9,815
  • 7
  • 43
  • 64
  • Runnning twice isn't a problem, but at the time of startup I need to call a function which needs the 'request' object. Will that be possible? – Himanshu Jan 28 '13 at 06:58