Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AuthorLogin(forms.Form):
- username = forms.CharField(label='Your name', max_length=100)
- password = forms.CharField(
- label='Your password',
- max_length=100,
- widget=forms.PasswordInput)
- def clean(self):
- username = self.cleaned_data.get('username')
- password = self.cleaned_data.get('password')
- user = authenticate(username=username, password=password)
- if not user or not user.is_active:
- raise forms.ValidationError('Invalid username or password', code='invalid')
- return self.cleaned_data
- def login(self, request):
- username = self.cleaned_data.get('username')
- password = self.cleaned_data.get('password')
- user = authenticate(username=username, password=password)
- return user
- def author_login(request):
- form = AuthorLogin(request.POST or None)
- if request.POST and form.is_valid():
- user = form.login(request)
- if user is not None:
- login(request, user)
- return redirect('microblog:index')
- return render(request, 'microblog/author_login.html', {'form': form})
- app_name = 'microblog'
- urlpatterns = [
- url(r'^$', views.index, name='index'),
- url(r'^login/', views.author_login, name='author_login'),
- url(r'^logout/', views.author_logout, name='author_logout'),
- ]
- {% extends "base.html" %}
- {% block content %}
- {% if form.non_field_errors %}
- <ul>
- {% for error in form.non_field_errors %}
- <li>{{ error }}</li>
- {% endfor %}
- </ul>
- {% endif %}
- <form action="" method="post">
- {% csrf_token %}
- {{ form.as_p }}
- <input type="submit" value="Login" />
- </form>
- {% endblock %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement