Example of how to use cookies in Django and djangocookie

Source: Internet
Author: User

Example of how to use cookies in Django and djangocookie

Preface

The emergence of various Internet-based service systems, the establishment of commercial sites or relatively well-functional personal sites, often need to record some of the visitor's information; forum as one of the products of Internet development, it plays an increasingly important role in the Internet and is one of the main places for users to obtain, exchange, and transmit information, forums often need to record some basic information of visitors (such as identity identification numbers, passwords, The way users shop on a Web site or the number of times they visit the site ). Currently, it is recognized that cookies and Session technologies are used to record some basic information of visitors.

Next, let's take a look at the basic usage of cookies in Django. If you don't talk about them much, let's take a look at the detailed introduction.

1. Brief Introduction

(1) set Cookies

response.set_cookie("cookie_key","value")

(2) obtain Cookies

value = request.COOKIES["cookie_key"]

(3) Delete Cookies

response.delete_cookie("cookie_key",path="/",domain=name)

(4) Cookies

if "cookie_name" in request.COOKIES :

(5)response.set_cookie()Pass some optional parameter descriptions

2. Example

2.1 set Cookies

Login_user = models. user. objects. get (username = username, password = password) # data storage in mongodb # print (login_user ["username"]) # The account and password are correct, cookie save logon status # Get the corresponding object response = redirect (reverse ("blog: index") # Set cookieresponse. set_cookie ("blog_username", login_user ["username"], 604800) # The expiration time unit is s (set to 7 days here) response. set_cookie ("blog_password", login_user ["password"], 604800)

2.2 detect and obtain Cookies

Def index (request): # Check whether cookies exist if "blog_username" in request. COOKIES: # obtain cookies login_username = request. COOKIES. get ("blog_username") login_password = request. COOKIES. get ("blog_password") # obtain the Login User Information login_user = models. user. objects. get (username = login_username, password = login_password) # return to the page after successful logon return render (request, "blog/index.html", {"login_user": login_user}) else: # return render (request, "blog/index.html ")

2.3 Delete Cookies

# Logout view function def logout (request): response = redirect (reverse ("blog: index") response. delete_cookie ("blog_username") response. delete_cookie ("blog_password") return response

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.