Gábor Erdős
Gábor Erdős

Reputation: 3689

Django new session for each browser tab

I have a problem with Django session and i have no idea how to solve it. Basically i store some data in the session dictionary in one view function, and calculate some thing in a second function using the values from the first view. Now if someone opens up two tabs, fills in the data in one and submits it, fills the second tab and submits it, the session dictionary will be the same for both tabs. I hope i phrase myself right.

Simple explanation:

def a(request):
    request.session["q"] = request.POST.get('q')

def b(request):
    while True:
        print(request.session["q"])

So lest assume function a is rendering an index page, and getting a value from there. On a button push in this index page function b is called. Now if i open two tabs, input 1 in to the index page and submit i ll see a lot of 1 in the terminal. Now i open up another tab, input 2, submit, the printing will change.

What i would like to do is to keep separate sessions (separate information to come in and go out) from my server to the user on different tabs of a browser.

I am sorry if i phrase myself wrong, this is the first time i am trying to work with web servers.

EDIT:

As i mentioned in the comments the answer is currently not working, and i think it is just a syntax error, however i dont know where i gone wrong.

My template:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
{% load staticfiles %}
<HTML>
<HEAD>
    <LINK rel="stylesheet" type="text/css" href="{% static 'style7.css' %}"/>
    <link rel="shortcut icon" type="image/png" href="{% static 'favicon.ico' %}"/>
    <script src="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.13.min.js"></script>
    <link rel="stylesheet" href="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.13.min.css">
<script type="application/javascript">function makeid() {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for (var i = 0; i < 5; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    return text;
}
 document.getElementById("session").value = makeid();</script>
</HEAD>

<BODY>
....
....
<form action="{% url 'raw' %}" method="post" style="float: left;">{% csrf_token %}<input type="hidden" name="session" id="session"><input type="submit" value="Download text"/></form>
....

My view function:

def plot(request):
    print(request.POST.get("session"))
    ....

However in the terminal see nothing printed, thus i think the variable is an empty string.

Upvotes: 0

Views: 2914

Answers (1)

mohammedgqudah
mohammedgqudah

Reputation: 568

i don't know what app you are making but lets say i want to save the username as session.

1. we need to create a script that create a random code and assign it to and hidden input

function makeid() {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for (var i = 0; i < 5; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    return text;
}

 window.onload = function() {
 document.getElementById("session").value = makeid();
 }

2. in the form that you take the data from user add a new hidden input

<form>
   ...
   <input type="hidden" name="session" id="session">
</form>

3. now when the user submit the form to a view we need to do this in the view

def someview(request):
    session_id = request.GET.get('session')
    username = request.GET.get('username') # or any data...
    request.session['username_%s' % session_id] = username
    # now lets say you want to redirect the user to the next page... 
    # you have to send the 'session_id' to it and always send to
    # the next view then in that view retrieve his name like this
    # ``name = request.session['username_%s' % session_id] ``
    # the session_id from the old view

so the urls should be like this:

/first-step/?session=somecode.

/second-step/?session=somecode

if you have more fields you have to save them in session and retrieve them as i did, hope it helps you, i know its complex! bye :)

Upvotes: 2

Related Questions