Reputation: 35682
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 django.http import HttpResponse
def main(request, template_name='index.html'):
HttpResponse.set_cookie('logged_in_status', 'zjm1126')
context ={
'a':a,
'cookie':HttpResponse.get_cookie('logged_in_status'),
}
return render_to_response(template_name, context)
#return http.HttpResponsePermanentRedirect(template_name)
It raises this exception:
unbound method set_cookie() must be called with HttpResponse instance as first argument (got str instance instead)
What can I do?
Upvotes: 55
Views: 102905
Reputation: 12627
In case you want the raw cookie string (tested with Django 4.1)
request.META.get('HTTP_COOKIE')
Upvotes: 3
Reputation: 118518
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.
Upvotes: 155