Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #models.py in main/ app
- class Mileage(models.Model):
- owner = models.ForeignKey(User)
- #views.py in accounts/ app
- def main(request):
- print (request.user.is_authenticated())
- title = "Login"
- form = UserLoginForm(request.POST or None)
- context = {
- "title": title,
- "form": form,
- }
- if form.is_valid():
- username = form.cleaned_data.get("username")
- password = form.cleaned_data.get("password")
- user = authenticate(username=username, password=password)
- login(request,user)
- print (request.user.is_authenticated())
- return redirect('home/')
- context = {
- "title": title,
- "form": form,
- }
- return render(request, "main.html", context)
- #forms.py in accounts/ app
- User = get_user_model()
- class UserLoginForm(forms.Form):
- username = forms.CharField()
- password = forms.CharField(widget=forms.PasswordInput)
- def clean(self, *args, **kwargs):
- username = self.cleaned_data.get("username")
- password = self.cleaned_data.get("password")
- if username and password:
- user = authenticate(username = username, password = password)
- if not user:
- raise forms.ValidationError("This user does not exist")
- if not user.check_password(password):
- raise forms.ValidationError("Incorrect password")
- if not user.is_active:
- raise forms.ValidationError("This user is no longer active")
- return super(UserLoginForm,self).clean(*args, **kwargs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement