Created
August 19, 2014 05:55
-
-
Save hitsujixgit/cbe0cb4235e2b7a177c7 to your computer and use it in GitHub Desktop.
Implemented add-user 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 %} | |
Add user form | |
{% endblock %} | |
{% block h1 %} | |
Add user form | |
{% endblock %} | |
{% block article_content %} | |
<form method="post" action="{{ add_user }}"> | |
{% csrf_token %} | |
<table> | |
{{ form.as_table }} | |
</table> | |
<input type="submit" class="button" value="Login" /> | |
</form> | |
<p><a href="/">Return to home</p> | |
{% 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.contrib.auth.models import User | |
from django import forms | |
from django.contrib.auth.decorators import login_required | |
@login_required | |
def page(request): | |
if request.POST: | |
form = MyUserCreationForm(request.POST) | |
if form.is_valid(): | |
username = request.POST['username'] | |
password = request.POST['password'] | |
email = request.POST['email'] | |
new_user = User.objects.create_user(username = username, email = email, password=password) | |
new_user.is_active = True | |
new_user.save() | |
return render(request, 'jp/add_user_successful.html') | |
else: | |
form = MyUserCreationForm() | |
return render(request, 'jp/add_user.html', {'form' : form}) | |
class MyUserCreationForm(forms.Form): | |
username = forms.CharField(label = "User name") | |
password = forms.CharField(label = "Password", widget = forms.PasswordInput) | |
password_bis = forms.CharField(label = "Password", widget = forms.PasswordInput) | |
email = forms.EmailField(label = "Email") | |
def clean(self): | |
cleaned_data = super(MyUserCreationForm, self).clean() | |
username = self.cleaned_data.get('username') | |
password = self.cleaned_data.get('password') | |
password_bis = self.cleaned_data.get('password_bis') | |
if password and password_bis and password != password_bis: | |
raise forms.ValidationError("Passwords are not identical.") | |
user_check = User.objects.filter(username = username).exists() | |
if user_check: | |
raise forms.ValidationError("Duplicate user name!") | |
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
{% extends "base.html" %} | |
{% block title_html %} | |
Add user successful | |
{% endblock %} | |
{% block h1 %} | |
Add user successful | |
{% endblock %} | |
{% block article_content %} | |
<p><a href="/">Return to home</p> | |
{% 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
{% extends "base.html" %} | |
{% block title_html %} | |
Django Test Page | |
{% endblock %} | |
{% block h1 %} | |
Hello world | |
{% endblock %} | |
{% block article_content %} | |
<p>Welcome to Django world!</p> | |
<p><a href="login">Your Account</a></p> | |
<p><a href="logout">Logout</a></p> | |
<p><a href="add_user">Add new user</a></p> | |
{% 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
from django.conf.urls import patterns, include, url | |
from django.contrib.auth.decorators import login_required | |
#from django.views.generic import TemplateView | |
from django.contrib import admin | |
admin.autodiscover() | |
urlpatterns = patterns('', | |
url (r'^add_user/$', 'myapp.views.add_user.page', name="add_user"), | |
url(r'^logout/$', 'myapp.views.logout.page', name="user_logout"), | |
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