User Management Section of Django's blog website (using its own django. contrib. auth) 1. Registration Section, django. contrib. auth
Project tool: Python 2.7.11 Django 1.10.2 Bootstrap 3.3.0 IDE: eclipse Pydev
1. Make sure that 'django. contrib. auth' already exists in settings'
For example:
Settings in myproject
1 INSTALLED_APPS = [ 2 'account', 3 'django.contrib.admin', 4 'django.contrib.auth', 5 'django.contrib.contenttypes', 6 'django.contrib.sessions', 7 'django.contrib.messages', 8 'django.contrib.staticfiles', 9 'blog',10 ]
MIDDLEWARE, and TEMPLATES with AuthenticationMiddleware and 'django. contrib. auth. context_processors.auth ', etc.
Generally, a Django project created with Pydev is automatically added.
2. Add urls
2.1 Add a master url:
Add
1 from django.conf.urls import url, include 2 from blog import views as blog_views 3 4 urlpatterns = [ 5 url(r'^admin/', admin.site.urls), 6 7 url(r'^blog/', include("blog.urls")), 8 9 url(r'^account/', include("account.urls")),10 ]
2.2 add
1 from django. conf. urls import url 2 from django. conf. urls. static import static 3 from django. contrib. auth import views as auth_views 4 from account import views as account_views 5 6 7 urlpatterns = [8 url (r '^ $', account_views.account_info, name = 'account '), 9 url (R' ^ edit/$ ', account_views.account_edit, name = 'accountedit'), 10 url (R' ^ register/$ ', account_views.register, name = 'register '), 11 12 # use the System Built-in auth: login, logout, password_change, password_change/done13 # password_reset, password_reset/done, 14 # login/logout urls15 url (r '^ login/$', auth_views.login, name = 'login'), 16 URLs (R' ^ logout/$ ', auth_views.logout, name = 'login '), 17 url (R' ^ logout-then-login/$ ', auth_views.logout_then_login, name = 'logout _ then_login '), 18 19 # change password urls20 url (R' ^ password-change/$ ', auth_views.password_change, Name = 'password _ change'), 21 url (R' ^ password-change/done/$ ', auth_views.password_change_done, name = 'password _ change_done '), 22 23 # restore password urls24 url (R' ^ password-reset/$ ', auth_views.password_reset, name = 'password _ reset '), 25 url (R' ^ password-reset/done/$ ', auth_views.password_reset_done, name = 'password _ reset_done'), 26 url (R' ^ password-reset/confirm /(? P <uidb64> [-\ w] + )/(? P <token> [-\ w] +)/$ ', auth_views.password_reset_confirm, name = 'password _ reset_confirm '), 27 url (R' ^ password-reset/complete/$ ', auth_views.password_reset_complete, name = 'password _ reset_complete'), 28]
3. Complete the models section:
from django.db import modelsfrom django.conf import settings
1 class Profile (models. model): 2 3 user = models. oneToOneField (settings. AUTH_USER_MODEL) 4 image = models. imageField (upload_to = 'static/image/% Y/% m', default = 'static/image/default.jpg ', max_length = 200, 5 blank = True, null = True, verbose_name = 'user Avatar ') 6 created_dt = models. dateTimeField (auto_now_add = True, db_index = True) 7 date_of_birth = models. dateField (blank = True, null = True) 8 9 def _ unicode _ (self): 10 return self. user. username
The external link User table method is used here. Use Django's own auth. You can also use the AbstractUser method that inherits the User. This method will be used later.
4. complete registration:
4.1 forms required to complete registration
1 class UserRegForm (forms. modelForm): 2 password = forms. charField (label = 'Password', widget = forms. passwordInput) 3 password2 = forms. charField (label = 'Repeat password', widget = forms. passwordInput) 4 5 class Meta: 6 model = User 7 fields = ('username', 'email ') 8 9 def clean_password2 (self): 10 cd = self. cleaned_data11 if cd ['Password']! = Cd ['password2 ']: 12 raise forms. ValidationError (u "Incorrect password verification") 13 return cd ['password2']
4.2 complete the registered views
1 # user registration 2 def register (request): 3 if request. method = "POST": 4 user_form = UserRegForm (request. POST) 5 if user_form.is_valid (): 6 new_user = user_form.save (commit = False) 7 new_user.set_password (user_form.cleaned_data ['Password']) 8 new_user.save () 9 profile = Profile. objects. create (user = new_user) 10 messages. success (request, u "User Registration successful") 11 return HttpResponseRedirect (reverse ('login') 12 else: 13 user_form = UserRegForm () 14 return render (request, 'account/register.html ', {'regform': user_form })
4.3 complete the template required for registration
{% Extends "account_base.html" % }{% block title %} registered user {% endblock % }{% block content %} <div id = "container"> <div id =" regform "> {% if regform. errors %} <p id = "erroralarm"> Please correct the error {regform. errors | pluralize} below. </p >{% endif %} <form action = "" method = "post"> <table border = 2 >{{ regform. as_table }}</table> <input type = "submit" name = 'register 'value = "register"> </form> </div> <div id = "protocoldiv" style = "display: none "> </div> <div id =" logoimg "> </div> {% endblock content %}
Here, account_base.html will be added separately:
The Code is as follows:
1 {% load staticfiles %} 2 <! DOCTYPE html> 3
Now the registration is complete.