0% found this document useful (0 votes)
22 views10 pages

Shahid Har

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views10 pages

Shahid Har

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

“JNANA SANGAMA”, BELAGAVI - 590018

K.R PET KRISHNA GOVERNMENT ENGINEERING COLLEGE,


K.R PET -571426
Department of Computer Science & Engineering
Presentation on
“Cookies”
Presented by
SHASHIDHAR 4GK22CS406

Under the Supervision of

Dr Devika G Head of the Department


Assistant Professor Dr. Hareesh K
Department of CSE Associate Professor and HOD
Cookies:
• A cookie is a small piece of information that browsers store on behalf of Web servers. Every time
a browser requests a page from a certain server, it gives back the cookie that it initially received.

• Django provide built in method to set and get cookies. The set cookie is used to set a cookie and
get cookie is used to get a cookie.

• Its is used to store user’s data in a file permanently.

• Cookie has its expiry date and removes automatically when gets expired.
Getting and Setting Cookies
• Reading cookies that are already set is incredibly simple. Every request object has a COOKIES
object that acts like a dictionary; we can use it to read any cookies that the browser has sent to the
view:

• def show_color(request):

if "favorite_color" in request.COOKIES:

return HttpResponse("Your favorite color is %s" % \

request.COOKIES["favorite_color"]) else:

return HttpResponse("You don't have a favorite color.")


• Writing cookies is slightly more complicated. We need to use the
set_cookie() method on an HttpResponse object. Here’s an example that
sets the favorite_color cookie based on a GET parameter:
def set_color(request):
if "favorite_color" in request.GET:
response = HttpResponse("Your favorite color is now %s" % \
request.GET["favorite_color"])
response.set_cookie("favorite_color", request.GET["favorite_color"]) r
else:
return HttpResponse("You didn't give a favorite color.")
Setting Test Cookies
• We can’t rely on every browser accepting cookies. So, as a convenience, Django provides an easy way to test
whether a user’s browser accepts cookies.

• Just need to call request.session.set_test_cookie() in a view, and check request.session.test_cookie_worked() in a


subsequent view—not in the same view call.

• def login(request):

# If we submitted the form... if

request.method == 'POST’:

# Check that the test cookie worked (we set it below):

if request.session.test_cookie_worked():

# The test cookie worked, so delete it.

request.session.delete_test_cookie()
THANK YOU

You might also like