jundymek
jundymek

Reputation: 1043

Django - store globally some values

I would like to store globally number of sites in my database. In my index view I have something like this:

sites_inactive = Site.objects.filter(is_active=False)
sites_all = Site.objects.all()
context['sites_inactive'] = sites_inactive.count()
context['sites_all'] = sites_all.count()

I would like to have an access to these variables in my every view. Now I must repeat my code in every view. Is it possible to store these values and simply put it in my base.html file? I mean:

Number of sites: {{ sites_all }}

Upvotes: 0

Views: 177

Answers (5)

jundymek
jundymek

Reputation: 1043

I made it that way.

processor.py

def my_context_processor(request):

    return {'sites_all': Site.objects.all().count,
            'sites_inactive':     Site.objects.filter(is_active=False).count()}

settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR, ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'mainapp.processor.my_context_processor',
            ],
        },
    },
]

Now I have access to sites_all and sites_inactive everywhere in my app. Is it ok?

Upvotes: 0

e4c5
e4c5

Reputation: 53774

Generally it's a bad idea to litter your code with globals. You should create a context processor as RemcoGerlich has already suggested, however instead of fetching values from the global, you should rely on a cache

def my_context_processor(request):

    obj = cache.get('site_stats')
    if not obj:

        sites_inactive = Site.objects.filter(is_active=False)
        sites_all = Site.objects.all()
        obj = {'sites_inactive':  sites_inactive.count(),
               'sites_all']: sites_all.count()}
        cache.set('site_stats',obj)

     return obj

Upvotes: 2

erikgaal
erikgaal

Reputation: 398

One way of doing this, is to use a custom ContextMixin and use that in every view. The custom ContextMixin should extend the get_context_data function.

class SiteMixin(ContextMixin):
    def get_context_data(self, **kwargs):
        context = super(SiteMixin, self).get_context_data(**kwargs)
        # Edit context
        return context

class MyView(SiteMixin, View):
    pass

Upvotes: 0

RemcoGerlich
RemcoGerlich

Reputation: 31260

If you use RequestContext to send context to your templates, then you can write your own context processor that adds those variables to the context, and add that to the 'processors' part of your TEMPLATES setting, then the variables will be available in every template.

Upvotes: 3

Darshit
Darshit

Reputation: 430

Django session is what you are looking for. You can store global data in session and access it in every view by using request.session.get('num_of_sites'). Check out this link for more information.

If you want to display it in every template better idea is to write context processor for that.

Upvotes: -1

Related Questions