Advertisement



< Prev
Next >



Django with Cookies



In this tutorial, we are going to explain how to create and maintain a user session by using cookies. A cookie is one of the most common ways to track a user on the internet. Cookie is a file using which you can store the information in a user's browser and once it is done, you can access it when you need to communicate with a user or to manage a user's session.

You can even specify the duration for which this cookie file is going exist before it gets deleted by the browser.

So let us create a Django project to explain you how to maintain a user session with cookies using Django Framework. To elaborate this, let us create a new Django project.








Creating the welcome.htm webpage


Once a user has correctly filled the form and have submitted it, a welcome.htm webpage is displayed to the user, displaying the profile created for the user after a successful login. This webpage will be stored in the folder of our application, cookies.

thanks.htm
<html>
<head><title>Django With Forms - Decodejava.com</title></head>
<body>
<b>Your Profile:</b> 
<br/>
<br/>
Welcome {{ f_n }},  <br/>
Your city =  {{ city }} <br/>
Your country = {{ country }}<br/>
Your age = {{ age }}<br/>
Your email = {{ email }} <br/>
Are you married? = {{ married }} <br/>
<br/>
You are logged in!
Thank you for submitting the form.
<br/>
<a href="logout/">Log out?</a>
</body>
</html>





Mapping URL to view


After creating the view functions of an application, we need to specify the mapping between a URL and each view function in a file named - urls.py. The file urls.py is simply coded in Python and we will have to create the urls.py file in the folder of our application, cookies.

The use of mapping a URL to the view function is, when this URL is requested by the user, its mapped view function is executed and a response is returned.

urls.py
from django.urls import path
from . import views

urlpatterns = [
        path('', views.get_form),
        path('form_submit/', views.save_read_cookies), 
        path('form_submit/logout/', views.delete_cookies),
]

As you can see in the file urls.py, we have imported the path() function from the module django.urls. The path() function has been passed the two must arguments -



Note:
This URL pattern specified in the application's urls.py file gets added to another URL pattern, specified in the project's urls.py file(we are going to create it next), and the combined path will be matched to the view functions defined in the application's views.py.



Pointing the project URL to application URL


Next, we are going to point the project URL(urls.py) to the application URL(urls.py) by using the include() function. For this, we will open the file urls.py in the root - project directory, django_with_cookie, and copy-paste the following code.

urls.py
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('welcome/template/', include('cookies.urls')),
]

As you can see in the file urls.py, we have imported the path() and include() function from the module django.urls.
The path() function has been passed the two must arguments -




How to load the HTML template file.


Next, we are going to show how to load the above created HTML template file template.htm, which is stored within our application folder cookies.

To load the HTML template file, we need to specify its full path in the configuration file - settings.py, which is stored in the project folder. For our example, you can find this file in the sub-directory django_with_cookie within our project django_with_cookie i.e. - django_with_cookie/django_with_cookie/settings.py.


Within the settings.py file, you need to locate the property DIRS and copy-paste the full-path to the HTML template file against it, as shown below and save the file.
'DIRS': ['E:/Django Projects/django_with_cookie/cookies'],

And finally, it's time to execute our Django project with templates.




Executing the application of our project


Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement