English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Sometimes, you may need to store some visitor data on each site as required by your web application. Always remember that cookies are stored on the client side, and the lifetime of the cookie is set according to the security level of your client browser, sometimes it may not be necessary.
To illustrate how cookies are handled in Django, let's create a system using the login functionality created earlier. The system will allow you to log in for x minutes, after which the application will automatically log you out of your login information.
For this, you need to set two cookies: last_connection and username.
Firstly, let's modify the login view to store the username and last_connection cookies -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 from django.template import RequestContext def login(request): username = "not logged in" if request.method == "POST": #Get the posted form MyLoginForm = LoginForm(request.POST) if MyLoginForm.is_valid(): username = MyLoginForm.cleaned_data['username'] else: MyLoginForm = LoginForm() response = render_to_response(request, 'loggedin.html', {'username': username}, context_instance = RequestContext(request)) response.set_cookie('last_connection', datetime.datetime.now()) response.set_cookie('username', datetime.datetime.now()) return response
As in the view above, cookies are set by calling the setcookie method instead of the response request, also note that all Cookie values are returned as strings.
Let's create a FormView for the login form, we will not display the form if the Cookie is set and10within seconds -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 def formView(request): if 'username' in request.COOKIES and 'last_connection' in request.COOKIES: username = request.COOKIES['username'] last_connection = request.COOKIES['last_connection'] last_connection_time = datetime.datetime.strptime(last_connection[:-7], "%Y-%m-%d %H:%M:%S") if datetime.datetime.now() - last_connection_time).seconds < 10: return render(request, 'loggedin.html', {'username': username}) else: return render(request, 'login.html', {}) else: return render(request, 'login.html', {})
You can access the Cookie you set on the formView view by requesting the COOKIES class attribute (dictionary).
Now modify the url.py file to change the URL, pair with the new view -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 from django.conf.urls import patterns, url from django.views.generic import TemplateView urlpatterns = patterns('myapp.views', url(r'^connection/','formView', name = 'loginform'), url(r'^login/', 'login', name = 'login'))
When visiting /myapp/connection, you will enter the following page-
After submission, it will be redirected to the following interface -
Now, if you are10seconds within access /myapp/connection Once, it will get a direct redirection to the second screen. If you visit it again /myapp/connection Out of this range, the login form (screen1)。