0

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 }}
jundymek
  • 1,013
  • 4
  • 18
  • 40

5 Answers5

3

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.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
2

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
e4c5
  • 52,766
  • 11
  • 101
  • 134
0

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
erikgaal
  • 398
  • 1
  • 11
0

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?

jundymek
  • 1,013
  • 4
  • 18
  • 40
-1

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.

Community
  • 1
  • 1
Darshit
  • 420
  • 2
  • 11
  • Yes. Session are unique per user. Better approach for this situation is to write custom context processor. – Darshit Jan 18 '17 at 12:19