0

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.

bigreddot
  • 33,642
  • 5
  • 69
  • 122
Gábor Erdős
  • 3,599
  • 4
  • 24
  • 56
  • hi erdos, [https://stackoverflow.com/questions/24121421/how-to-set-only-one-session-per-tab-browser](https://stackoverflow.com/questions/24121421/how-to-set-only-one-session-per-tab-browser) – mohammedgqudah Jan 04 '18 at 12:55
  • @mohammedqudah Hi, thanks i have found this thread before, and also tried to look into this, however i dont understand how it helps me. As far as i understand this is a JS command (i might be horribly wrong here) and i dont know in which template should i use it, should i do something inside Django etc – Gábor Erdős Jan 04 '18 at 12:57
  • @mohammedqudah I have also tried `request.sessionStorage["asd"] = 2`, but it gives an error, as sessionStorage does not exist. – Gábor Erdős Jan 04 '18 at 13:02
  • this is actually can be done using ajax and js and django view but it will make your code complex, you don't have to do it. but i can tell you how tomorrow because i have to sleep now :D, bye :) – mohammedgqudah Jan 04 '18 at 20:12
  • @mohammedqudah Thanks a lot, i am really stuck with this. – Gábor Erdős Jan 05 '18 at 09:54
  • please add the hidden input code – mohammedgqudah Jan 05 '18 at 18:19
  • i know the problem, i edited the script in my answer update your script please, this should work. – mohammedgqudah Jan 05 '18 at 18:29

1 Answers1

2

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 :)

mohammedgqudah
  • 568
  • 4
  • 15
  • I understand the method, and i think it will work in my case as well, however i think i have some problem with my syntax. I added the javascript snippet, the hidden input, however i cant print out the generated random string in the view. I ll update my answer with my codes, i am pretty sure its just a silly syntax error, but i dont know anything about JS. Thanks a lot! – Gábor Erdős Jan 05 '18 at 16:06