0

Let me give a example.My views.py is like this.

def view1(request):
  if request.method == 'POST':
    #gather form put in a list
    li=[form_value1,form_value2]
    #do something with li
    return HttpResponse()# Have to use HttpResponse
    # in that there is a link of view2 
 return render(request,'form.html',{})
def view2(request):
  #here i want to use li again

Can I declear it as global

def view1(request):
  global li
  li=[form_value1,form_value2]

or can I use like this

li = []
def view1(request):
  li.append(form_value1)
def view2(request):
  #extract li 

In both case I got unsuccessful So please guide me. Thanks in advance

2
  • What are those form_values? If you're getting them inside the view method how do you expect they'll be available globally? Commented Mar 25, 2014 at 13:01
  • They are simple integer. So what can be the solution. Commented Mar 25, 2014 at 13:03

3 Answers 3

2

Sessions are the easiest way to achieve this:

def view1(request):

    if request.POST: 
        request.session['li'] = [form_value1,form_value2]
        return HttpResponse()

    return render(request,'form.html',{})

def view2(request):

    li = request.session['li']

    ...

https://docs.djangoproject.com/en/dev/topics/http/sessions/

Sign up to request clarification or add additional context in comments.

4 Comments

I am Little bit new to web devlopment. So m I have to add anything in seting .py. I am using django 1.6. I have not active session. So only that much will work? I have not changed anything from default
You just need to make sure you have django.contrib.sessions in INSTALLED_APPS in settings.py
Can I declear & assign li 1st, Then do session request.session['li']
Yes. In view1 li = [form_value1,form_value2] then request.session['li'] = li then in view2 li = request.session['li']
1

You need to pass the value back in the response to the client and then back again to the server when they request page 2. That could be done via cookies, hidden form fields, embedded in Javascript or a number of other options.

There are ways around this but all of them require your server being stateful.

The next best option is the concept of a session. This is a per-user storage space with a limited lifetime (usually extended to -say- 20 minutes every time a request comes in from that user). You could store user-specific variables there. This mitigates the threading issues and limits the wasted server resources. A client is given a unique id (usually in a cookie) which is used to look up their session data on the server. See Django Sessions for more information.

If you made a variable global, it would apply to all users of your site and would be accessed from multiple threads (which means you need to understand about all the usual threading problems like race conditions).

2 Comments

Thank You for your answer. The thing is how can I send that data(may be hidden input) with httpresponse
@Swagat You're welcome. I just added a little bit about sessions which may be the compromise you need
0

li will be recreated for each request as is the nature of web servers which are stateless. Each view handles a request and so li will not be available to the next. You could use memcached to store the value of li and retrieve it in the other view or you could look at sessions.

2 Comments

I don't believe this is true for Django as each module is loaded only once (on startup) then the methods are called on demand. So technically module-level code outside of methods should be server-wide, but I agree it's not the right way to do things and doesn't translate to other servers
Your right. Have a look at this thread groups.google.com/forum/#!topic/django-users/c2ZwrzP_0gs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.