This interests me:

https://teamtreehouse.com/community/django-how-can-i-retrieve-a-value-from-the-middleware

alas no answer there, and time has changed things.

I have checked form experience, that stuff added to response.context_data in middleware is not seen in templates (perhaps comes too late!).

Another pager here:

https://mlvin.xyz/django-templates-context-processors.html

describes how to add to context used context processors. Searching the web is nightmare in this space because of how Django has changed over time and the different methods in different versions.

The problem I have with that sample above is he's replicating code by including in settings.py:

TEMPLATES  =  [
    {
        'BACKEND':  'django.template.backends.django.DjangoTemplates',
        'DIRS':  [os.path.join(BASE_DIR,  "templates"),],
        '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',
                # our custom context processor
                'utils.context_processors.active_shows',
            ],
        },
    },
]


most of which is standard. Is there not a much more DRY way to add a context processor? Would this work?

TEMPLATES  =  [
    {
        'OPTIONS':  {
            'context_processors':  [
                'utils.context_processors.active_shows',
            ],
        },
    },
]


Probably not at a guess.  But is there a nicer way to do it?

Another way might be to patch the response.content in the middleware but that too seems messy.

Surely there's a nice way for middlware to take some timing stats around the:

        response = self.get_response(request)

invocation that typically returns a response (and which I presume has already been through the whole template processing and has finalised response.content by the time it's done, so taking a timestamp after it's done is easy but making the result available in the templates via a context variable like {{execution_time}} would seem structurally impossible if context processing has already happened.

I can only imagine a trick using another context key like %%execution_time%% say and replacing that response.content.

I wonder if I'm on the right track here? Or if someone has a better idea?

Regards,

Bernd.


--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ef492997-0a93-2fa0-0c03-1403cd772f5b%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to