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 7051665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:17:26+00:00 2026-05-28T03:17:26+00:00

I realize that using: … return render_to_response(‘mytemplate.html’, locals(), context_instance=RequestContext(request)) in views is not considered

  • 0

I realize that using:

...
return render_to_response('mytemplate.html',
locals(), context_instance=RequestContext(request))

in views is not considered good code and something like:

...
return render_to_response('mytemplate.html', {
        'some_variable' : some_variable,
        'some_list': some_list,
}, context_instance=RequestContext(request)) 

is considered better for its legibility and explicitness. I was just curious how best to deal with variables which may or may not be returned. Should I explicitly set them in the views like this:

...
some_variable = None
some_variable = <some business logic>
return render_to_response('mytemplate.html', {
        'some_variable' : some_variable,
        'some_list': some_list,
}, context_instance=RequestContext(request)) 

which would result in more lengthy view code. Or should I check for existence of the variables before including them in the response?

Of course if I do nothing then I get:

local variable 'some_variable' referenced before assignment

Any suggestions welcomed.

  • 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-28T03:17:27+00:00Added an answer on May 28, 2026 at 3:17 am

    A middle way is to use the context dictionary itself as a stack.

    context = {}
    if <condition>:
        context['cond1'] = 'foo'
    
    if <condition2>:
        context['cond2'] = 'bar'
    
    return render_to_response('template.html', context)
    

    (Also note that since Django 1.3 you can use render(request, template, context) instead of the longwinded context_instance=RequestContext stuff.)

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

Sidebar

Related Questions

today i realize that some pages can locate me (im using a laptop with
I realize that if you are having a same selection in JComboBox, using up/down
I realize that, generally speaking, there are performance implications of using reflection. (I myself
After using F# option type for a while, I realize that it could be
I just came to realize that my project is currently using methods that are
Any ideas on an async directory search using fs.readdir? I realize that we could
I realize that similar questions have been asked before. My list is displayed using
I'm using MKAnnotationView for pins & callouts and I realize that there is the
I'm using django and realized that when the filename that the user wants to
I am testing my webpage on a server using preview dns. Just realized that

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.