pythondjangodjango-views

Django does not reset module variables during multiple requests


It seems that module variables live as long as a process lives and do not reset until the process restarts.

Here is my code which i expect to behave another way that it behaves now:

I have a module responsible for various SEO features like breadcrumbs and title, file fancy/utils.py:

class Seo:
    title = ['project name']

Later in my views i can add items to Seo.title (news.views for example):

from fancy.utils import Seo

def index(request, news_id):
    title.append('some specific title')
    ...

The point is that the variable Seo.title actually does not reset at every request, so it continues to append items to itself and it looks very strange to me (as i came from PHP).

Eventually if i hit F5 at the same page the title always grows up to smth huge and long.

What's going on and what should i do?

thanks


Solution

  • It seems from your comments that you have totally misunderstood the execution model of Django.

    You can't have data local to the request that you can somehow access from anywhere in the code. If you need data associated with a particular request, you should store it somewhere where the code running that request can retrieve it: perhaps in the session, perhaps in a temporary dictionary added onto the request object itself. Anything you store globally will be, well, global: visible to any request running inside the same process.