Created
August 19, 2014 05:24
-
-
Save hitsujixgit/b002b2bb584391911e3b to your computer and use it in GitHub Desktop.
Implemented login function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "base.html" %} | |
{% block title_html %} | |
User login form | |
{% endblock %} | |
{% block article_content %} | |
{% if user.is_authenticated %} | |
<!-- Authenticated. --> | |
<h1>You are connected.</h1> | |
<p>Your email : {{ user.email }}</p> | |
<p><a href="/">Return to home</a></p> | |
{% else %} | |
<!-- Not authenticated --> | |
<h1>User login form</h1> | |
<form method="post" action="{{ user_login }}"> | |
{% csrf_token %} | |
<table> | |
{{ form.as_table }} | |
</table> | |
<input type="submit" class="button" value="Login" /> | |
</form> | |
{% endif %} | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- Coding: utf-8 -*- | |
from django.shortcuts import render, redirect | |
from django import forms | |
from django.contrib.auth import authenticate, login | |
def page(request): | |
if request.POST: | |
form = Login_form(request.POST) | |
if form.is_valid(): | |
username = request.POST['username'] | |
password = request.POST['password'] | |
user = authenticate(username=username, password=password) | |
if user is not None: | |
if user.is_active: | |
login(request, user) | |
if request.GET.get('next') is not None: | |
# Redirect to a success page. | |
return redirect(request.GET['next']) | |
# New form when not the request is get. | |
else: | |
form = Login_form() | |
return render(request, 'jp/login.html', {'form' : form}) | |
class Login_form(forms.Form): | |
username = forms.CharField(label="User name") | |
password = forms.CharField(label="Password", widget=forms.PasswordInput) | |
def clean(self): | |
cleaned_data = super(Login_form, self).clean() | |
username = self.cleaned_data.get('username') | |
password = self.cleaned_data.get('password') | |
if not authenticate(username=username, password=password): | |
raise forms.ValidationError("Wrong user name or passwsord") | |
return self.cleaned_data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Authentification | |
LOGIN_URL = 'user_login' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.decorators import login_required | |
from django.views.generic import TemplateView | |
urlpatterns = patterns('', | |
url(r'^login/$','myapp.views.login.page', name="user_login"), | |
url(r'^$', login_required(TemplateView.as_view(template_name='jp/index.html')), name="index"), | |
#url(r'^$', 'myapp.views.index.page', name="index"), | |
url(r'^admin/', include(admin.site.urls)), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment