On 5 Jan, 11:41, Deniz Dogan <deniz.a.m.do...@gmail.com> wrote:
> On 5 Jan, 10:47, Deniz Dogan <deniz.a.m.do...@gmail.com> wrote:
>
>
>
> > Hi
>
> > I'm having trouble when using the AuthenticationForm in Django. It
> > tells me that I haven't enabled cookies, even though I'm completely
> > sure that I have. I get this error:
>
> > "Your Web browser doesn't appear to have cookies enabled. Cookies are
> > required for logging in."
>
> > I've tried restarting Django, the browser, tried several different
> > browsers, etc. I'm completely lost. I read somewhere that the login
> > view has to be called with a GET request before it's called with a
> > POST for this to work, but it *is* called with a GET to actually view
> > the login form in the first place.
>
> > Below is the login view that I'm using.
>
> > ---------------------------------
>
> > def user_login(request):
>
> >     if request.user.is_authenticated():
> >         return HttpResponseRedirect('/main/')
>
> >     if request.method == 'POST':
> >         form = AuthenticationForm(request.POST)
> >         if form.is_valid():
> >             login(request, form.get_user())
> >             return HttpResponseRedirect('/main/')
> >         else:
> >             return render_to_response('login.html', { 'form' : form })
>
> >     else:
> >         form = AuthenticationForm()
> >         return render_to_response('login.html', { 'form' : form })
>
> > ---------------------------------
>
> > Thanks,
> > Deniz
>
> I finally managed to help the problem by doing AuthenticationForm
> (None, request.POST). I have understood that None should represent the
> HTTPRequest object so that the form can validate that cookies have
> been enabled. Passing None bypasses this check, but is there any real
> danger in doing this?
>
> Thanks again,
> Deniz

I think I got it now, so if anyone has the same problem, this is what
happens...

When the user first reaches the actual login page (by HTTP GET), you
must call request.session.set_test_cookie() which will help
AuthenticationForm later determine whether cookies are enabled on the
user's side. AuthenticationForm checks so that the test cookie has
been set in the POST request from the user and will raise
ValidationError if it's not. So to put it short, when the user reaches
the login page, do request.session.set_test_cookie(), otherwise you
cannot properly test if cookies have been enabled. Of course, if you
don't care if the user has enabled cookies (for whatever reason that
may be), you can just pass None as the first parameter to the
constructor.

Deniz
--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to