Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7675925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:04:50+00:00 2026-05-31T17:04:50+00:00

What is the best practice for writing a thread-safe context processor in Django? Say,

  • 0

What is the best practice for writing a thread-safe context processor in Django?

Say, I want to pass some variables to templates, which
are set in the corresponding views, and could be different for
different view-template pairs.

One solution would be to manually pass each variable in the context:

return render_to_response('template.html', {'var1':var1,... 'var10':var10},
                           context_instance=RequestContext(request))

To keep it DRY, however, I would rather use a context processor. But I
worry about thread safety as it seems to require a global store.
Here is my solution using a context processor, which ties each
variable to the request. Thanks for your comments and suggestions.

In context_processor.py:

store = {}
def add_context(request, key, value):
    if request not in store:
        store[request] = {}
    store[request][key] = value
    return
def misc_context_processor(request):
    return store.pop(request,{})

In views.py:

import context_processor
def view(request):
    ...
    if x == y:
        context_processor.add_context(request,'var1','value1')
    else:
        context_processor.add_context(request,'var2','value2')
    ...
    return render_to_response('template.html', {},
                              context_instance=RequestContext(request))

In settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    ...,
    'appname.context_processor.misc_context_processor',
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:04:51+00:00Added an answer on May 31, 2026 at 5:04 pm

    context_processors are for context variables you want set up in every view to be available for every template. If you have view specific context, that rightfully belongs in the view. If you’re trying to push the construction of view specific context dictionaries off to a context_processor then your really creating an unnecessary headache and a landmine if anyone else ever has to touch your code. Use the tools for what they are meant to be used for.

    Additionally it’s a lot easier to write and read:

    context = {
        'var1': value1,
        'var2': value2,
    }
    

    than it is to try to figure out what this is doing:

    context_processor.add_context(request, 'var1', value1)
    context_processor.add_context(request, 'var2', value2)
    

    Or maybe what your want is like this:

    def view(request):
        context = {}
        ...
        if x == y:
            context['var1'] = value1
        else:
            context['var2'] = value2
        ...
        return render_to_response('template.html', context,
                context_instance=RequestContext(request))
    

    Or maybe even use context.update({ 'var1': value1 })

    I’m missing how the second one is more DRY. Considering you’re going to have to do that in every view that needs those variables anyway…

    If you have repeatable context generation use class based views to abstract that out in a reasonable fashion. If you really just have 10variables and each template only needs some of them (but they vary from template to template) then just simply make all of them available to all templates. As long as the generation isn’t expensive this works great, and keep in mind that querysets are lazy so if you never evaluate them they never hit the db

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for some advice on best practice when writing a web app that
What is the best practice for writing a rather large wcf service, containing a
Just checking if there's any best practice when writing a Windows Service. The Service
below content is taken from Best practice: Writing efficient code but i didn't understand
I'm wondering what is the best practice for writing #hashCode() method in java. Good
I'm writing a library and wonder what's the best practice for datatypes used in
Which of the following is best practice in Objective-C? UITableView* view = (UITableView*) [self
What's considered best practice writing OOP classes when it comes to using a property
As a beginning iPhone programmer, what is the best practice for writing apps to
For writing summary and parameter text, is there a best practice for how much

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.