How to set or get a cookie value in django

前端 未结 1 541
轮回少年
轮回少年 2020-12-25 11:27

This is my code:

from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django import http
from d         


        
相关标签:
1条回答
  • 2020-12-25 12:02

    You can't just start calling methods on the HttpResponse class, you have to instantiate it e.g. response = HttpResponse("Hello World"), call the cookie method, and then return it from your view.

    response = render_to_response(template_name, context)
    
    response.set_cookie('logged_in_status', 'never_use_this_ever') 
    return response
    # remember my other answer: 
    # it's a terrrible idea to set logged in status on a cookie.
    

    To get the cookie:

    request.COOKIES.get('logged_in_status') 
    # remember, this is a terrible idea.
    
    0 讨论(0)
提交回复
热议问题