tags:

views:

128

answers:

1

Here is my problem. DJango continues to store all the global objects after the first run of a script. For instance, an object you instantiate in views.py globally will be there until you restart the app server. This is fine unless your object is tied to some outside resource that may time out. Now the way I was thinking to correct was some sort of factory method that checks if the object is instantiated and creates it if it's not, and then returns it. However, the this fails because the object exists there since the last page request, so a factory method is always returning the object that was instantiated during the first request.

What I am looking for is a way to trigger something to happen on a per request basis. I have seen ways of doing this by implementing your own middleware, but I think that is overkill. Does anyone know of some reserved methods or some other per request trigger.

+5  A: 

Simple: Don't use global objects. If you want an object inside the view, instantiate it inside the view, not as global. That way it will be collected after the view ends.

nosklo
Also, you can use the session for objects that are "global" (actually persistent) until the user logs out.
S.Lott
There are good uses for module globals, but you have to be sure that they are immutable and valid for as long as the server remains up.
Carl Meyer
If they were immutable and valid for so long as the server is up, what stops you from putting it in settings.py
Lakshman Prasad