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

  • Home
  • SEARCH
  • 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 6089107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:03:44+00:00 2026-05-23T12:03:44+00:00

The issue is that in some views, I am manually getting a context variable

  • 0

The issue is that in some views, I am manually getting a context variable (let’s say “G”) of interest since I use it to find other information in that particular view (i.e.views A,B,C), but in other views (i.e. X,Y,Z), I need to get that particular context variable since this context is to be available in every single view in my project (since my base template uses the context variable). The issue with using a custom context processor is that I believe it will make an additional and IDENTICAL DB call in views (A,B,C) since those views are already getting that context variable since it’s needed to get other data in the view. What I was thinking was maybe I could implement a context processor that checks whether that specific context variable is set for a given request. Is this possible? Is there an easier solution? The code below may clarify the issue for some people.

Thank you for any advice!

def viewA(request):
    g=G.objects.get(user=request.user)
    posts = Post.objects.filter(g=g)
    return direct_to_template(request,'something.html',{'G':g, 'posts':posts})

def viewX(request):
    stuff = Albums.objects.get(user=request.user)
    return direct_to_template(request,'something2.html',{'stuff':stuff})

def my_context_processor(request): #redundant in case of viewA (hits db again?)
    return {'G':G.objects.get(user=request.user)} 

def ideal_processor(request):
    #check context vars to see if G is already in there
    #if it is, return {}, else, return {'G':G.objects.get(user=request.user)} 
  • 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-23T12:03:45+00:00Added an answer on May 23, 2026 at 12:03 pm

    I just made middleware that sets the variabel G to request.G since I need it on virtually every request anyway. i.e.:

    class GuildMiddleware(object):
        def process_request(self, request):
            request.G = figure_out_what_G_is()
            return None
    

    Now you can use request.G anywhere in your views (and templates if you’re using direct_to_template, RequestContext, etc.).

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

Sidebar

Related Questions

I have a design issue that I would appreciate some input on. I would
An interesting issue came up recently. We came across some code that is using
I resolved this issue myself. It turns out that the activation framework requires some
I have an other active question HERE regarding some hopeless memory issues that possibly
I'm having an issue where some of my views look incorrect on App load
Just want to get some views/possible leads on an issue I have. I have
I have an issue with the rendering of some views (buttons and edittext) which
I've got a simple little web app that's aggregating a couple views from some
In using our TeamCity Continuous Integration server we have uncovered some issues that we
I've just started using NHibernate, and I have some issues that I'm unsure how

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.