0

I am trying to count in my Django website. I use a global variable testc to count (I know this caused the problem, but I don't know how to do the counting without a global variable). So if two browser open the website (threadtest.html) in the same time, they will both increase the same testc (of course). Is there a way to do count individually in each browser session? Do I need to use multi-thread? Thanks.

Here is my view:

testc=0
def threadtest(request):
    global testc
    if request.method == 'POST':
        testc=testc+1
        print testc
    return render(request,'threadtest.html',{'count':testc,})

Here is the template: base.html

{% load staticfiles %}
{% load static %}
{% load extra_tags %}
<html>
<head>
<title>{% block title %}{% endblock %}</title>


{%block myscripts %}{% endblock %}

</head>
<body>
<div id="content">
    {% block content %}{% endblock %}
</div>

</body>
</html>

threadtest.html

{% extends 'base.html' %}
<html>
<head><title></title></head>

<body>

{% block content %}
<h1> Current count is </h1>
<h1> {{count}} </h1>
<form action="/threadtest/" method="post">{% csrf_token %}
<input type= "submit" name = "button" value = "addit" />
</form>
{% endblock %}
</body>
</html>
4

1 回答 1

1

You can get that info from:

request.META['HTTP_USER_AGENT']

Some output will be for example:

   Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.70 Safari/537.36

Maybe you can find helpful this snippet

于 2013-07-11T03:26:45.697 回答