lqdc
lqdc

Reputation: 531

Initializing a class at Django startup and then referring to it in views

I am trying to do some pre-processing at Django startup (I put a startup script that runs once in urls.py) and then use the created instance of an object in my views. How would I go about doing that?

Upvotes: 3

Views: 946

Answers (2)

Matthew Schinckel
Matthew Schinckel

Reputation: 35599

You can use a Context Processor to add it to your template context.

If you want it in the View, rather than the Template, then you can either have a base View class that has this, or just import the reference into the module your view is in (and access it directly).

Be aware that each django thread may have a different copy of the object in memory, so this should really only be used for read-only access. If you make changes to it, you are likely to find yourself in a world of hurt.

Upvotes: 0

LtWorf
LtWorf

Reputation: 7600

Try to use the singleton design pattern.

Upvotes: 2

Related Questions