Answer the question
In order to leave comments, you need to log in
Why don't cookies work under Pythom/Django?
Help a beginner. Something I don’t even understand why it doesn’t work:
View:
from django.http import HttpResponse
from django.shortcuts import render_to_response
def main_init ( request ) :
if "NumVisit" in request.COOKIES:
# стоят куки, и это не первый визит
NumViz = request.COOKIES["NumVisit"]
NumViz = int( NumViz ) + 1
else:
# куки не стоят
NumViz = 0
dimention_to_template = { 'NV': NumViz } # словарь, для передачи шаблону
return render_to_response( "index.html" , dimention_to_template ).set_cookie ("NumVizit", str( NumViz ) )
<p>{{ NV }}</p>
Answer the question
In order to leave comments, you need to log in
It actually works like this:
from django.shortcuts import render
def main_init ( request ) :
NumViz = 0
if "NumVisit" in request.COOKIES:
# стоят кукии, и это не первый визит
NumViz = request.COOKIES["NumVisit"]
NumViz = int( NumViz ) + 1
dimention_to_template = { 'NV': NumViz } # словарь, для передачи шаблону
return render( request, "index.html", dimention_to_template ).set_cookie( "NumVisit", NumViz )
Nalo get an object response and set him:
def view(request):
response = HttpResponse( 'blah' )
response.set_cookie( 'cookie_name', 'cookie_value' )
return response
the code is just awful
and doesn't work because set_cookie returns None
Read PEP08 already! set_cookie is called on response as above...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question