Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. class AuthorLogin(forms.Form):
  2. username = forms.CharField(label='Your name', max_length=100)
  3. password = forms.CharField(
  4. label='Your password',
  5. max_length=100,
  6. widget=forms.PasswordInput)
  7.  
  8. def clean(self):
  9. username = self.cleaned_data.get('username')
  10. password = self.cleaned_data.get('password')
  11. user = authenticate(username=username, password=password)
  12. if not user or not user.is_active:
  13. raise forms.ValidationError('Invalid username or password', code='invalid')
  14. return self.cleaned_data
  15.  
  16. def login(self, request):
  17. username = self.cleaned_data.get('username')
  18. password = self.cleaned_data.get('password')
  19. user = authenticate(username=username, password=password)
  20. return user
  21.  
  22. def author_login(request):
  23. form = AuthorLogin(request.POST or None)
  24. if request.POST and form.is_valid():
  25. user = form.login(request)
  26. if user is not None:
  27. login(request, user)
  28. return redirect('microblog:index')
  29.  
  30. return render(request, 'microblog/author_login.html', {'form': form})
  31.  
  32. app_name = 'microblog'
  33. urlpatterns = [
  34. url(r'^$', views.index, name='index'),
  35. url(r'^login/', views.author_login, name='author_login'),
  36. url(r'^logout/', views.author_logout, name='author_logout'),
  37. ]
  38.  
  39. {% extends "base.html" %}
  40.  
  41. {% block content %}
  42. {% if form.non_field_errors %}
  43. <ul>
  44. {% for error in form.non_field_errors %}
  45. <li>{{ error }}</li>
  46. {% endfor %}
  47. </ul>
  48. {% endif %}
  49. <form action="" method="post">
  50. {% csrf_token %}
  51. {{ form.as_p }}
  52. <input type="submit" value="Login" />
  53. </form>
  54. {% endblock %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement