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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:13:52+00:00 2026-06-12T14:13:52+00:00

In views.py When I try this one to access a global variable from other

  • 0

In views.py

When I try this one to access a global variable from other def:

def start(request):
    global num
    num=5
    return HttpResponse("num= %d" %num) # returns 5 no problem....

def other(request):
    num=num+1
    return HttpResponse("num= %d" %num)

def other does not return 6, but it should be 6 right ? How can I access a variable globally in my view ?

  • 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-06-12T14:13:54+00:00Added an answer on June 12, 2026 at 2:13 pm

    Use sessions. This is exactly what they are designed for.

    def foo(request):
       num = request.session.get('num')
       if num is None:
          num = 1
       request.session['num'] = num
       return render(request,'foo.html')
    
    def anotherfoo(request):
       num = request.session.get('num')
       # and so on, and so on
    

    If the session has expired, or num did not exist in the session (was not set) then request.session.get('num') will return None. If you want to give num a default value, then you can do this request.session.get('num',5) – now if num wasn’t set in the session, it will default to 5. Of course when you do that, you don’t need the if num is None check.

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

Sidebar

Related Questions

In one of my views the following function being called like def post_list(request, page=0,
Many people use this method to add animation on switching views. When I try
I'm a little stumped on this one. Anyone have any ideas? I'll try to
this is a follow up question from this one , I don't want to
I've a problem when I try to access the image property of one Core
when i try to view the databases in mysql i get this error: ERROR
I have the given view and template code, when i try to run this
I have this problem when i try to synchronize a observable list with listbox/view
I try to change the dimensions of the view in interface builder from the
I have two views: the first one is a UITableView in plain style (created

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.