Tuesday, November 22, 2011

Django : CSRF and variable handling in a view

Hello,

I've got a subscription form and this view :

def index(request):
c = RequestContext(request)
if request.user.is_authenticated():
return render_to_response('index.html', {'has_account': True})
if request.method == 'POST':
form = SignupForm(request.POST,error_class=DivErrorList)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = SignupForm()
return render_to_response('index.html', {'form': form, 'has_account':
False}, c)

1) In Index.html I have a form with a {% csrf_token %}. If I don't put
c = RequestContext(request) and add the c into every
render_to_response I've got a csrf error. Is my view above the right
way to handle csrf ?

2) I noticed that instead putting
c = RequestContext(request) *at the beginning of my view*
and :
return render_to_response('index.html', {'form': form, 'has_account':
False}, c) *at the end of my view*

I could just put this at the end of my view :

c = RequestContext(request, {'has_account': False,'form': form})
return render_to_response('index.html', c)

Which one it the best approach ?

3) I also noticed that if i don't pass 'has_account': False to my
template, nothing changes, it still evaluate it as false in {% if
has_account %}. Is it best to pass it to the template anyway ?

Thanks in advance,

Nolhian

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate