Monday, September 29, 2014

Re: at django login my request.POST.get data are all empty, I struggle with what I missed there

Use a login view like this:
@sensitive_post_parameters('password')
def login_view(request):
    form
= LoginForm(initial={'email': request.order.email})
   
if request.POST.get('login'):
        form
= LoginForm(data=request.POST)
       
if form.is_valid():
           
if user.is_active:
                auth
.login(request, form.user)
               
return redirect('public:courselist')
           
else:
                messages
.error(request, "Ihr Account wurde deaktiviert. Bitte nehmen Sie Kontakt zu uns auf, wenn Sie Ihnen wieder aktivieren wollen.")
               
return redirect('userauth:login')
   
return render(request, 'userauth/login.html', {'form': form})


and add a clean() method to your form like this:
    def clean(self):
       
if self.cleaned_data.get('username') and self.cleaned_data.get('password'):
           
self.user = authenticate(username=self.cleaned_data['username'], password=self.cleaned_data['password'])
           
if self.user is None:
               
raise forms.ValidationError('Please enter a correct username and password.')
           
elif not self.user.is_active:  # or leave this out and handle inactive case in your view above.
               
raise forms.ValidationError('This account is inactive.')
       
return self.cleaned_data


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f08c1fcc-b506-41fd-b8cb-2e6ac662257e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment