0
# In mypage.html
<form method='get' action=''>
    <input type="submit" value="add" name="add"/>
</form>
<h1>{{ p }}</h1>


# In the views.py file
p = 1
def mypage(request):
    global p
    my_dictionary = {
        "p" : p,
    }
    if request.GET.get('add'):
        p = p+1
        if 'p' in request.session:    
            request.session['p'] = request.session['p'] + 1
        else:
            request.session['p'] = 1
        my_dictionary = {
            "p" : p,
        }
    return render(request, "mypage.html", my_dictionary)

mypage.html essentially adds 1 to p when the button "add" is clicked. However I want the add to reset back to p=1 when the page is refreshed. How would I do that?
Thanks.

M.javid
  • 6,387
  • 3
  • 41
  • 56
PiccolMan
  • 4,854
  • 12
  • 35
  • 53

1 Answers1

-1

<input type="submit" value="add" name="add"/> is invalid. You can't have data on a submit button.

Add a <input type="hidden" name="add" value="add">. Instead.

Otherwise your usage is right. Sessions are per user, not shared.

Furthermore I'd recommend not using GET for this type of request. GET should only be used, if the request doesn't change anything on the server. This is might be a cornercase, but if you'd do the same with a model you should definitely use POST instead.

Chris Schäpers
  • 1,238
  • 10
  • 17