Django user registration and logon: Django User Registration

Source: Internet
Author: User

Django user registration and logon: Django User Registration

Django is a free open-source website framework developed by Python. It can be used to quickly build high-performance and elegant websites!

It is very difficult to learn django, and it is so difficult to get the simplest user logon and registration interface recently. At present, it is basically implemented. Although the function is particularly simple, it makes a record, further study will be provided in the future:

First, create a project and go to the project directory: django-admin startproject demo0414_userauth

Go to project: cd demo0414_userauth

Create the corresponding app: django-admin startapp account

Structure of the entire project

── Account
│ ── Admin. py
│ ── Admin. pyc
│ ── Apps. py
│ ── Init. py
│ ── Init. pyc
│ ── Migrations
│ ── 000w.initial.py
│ ── 000w.initial.pyc
│ ── Init. py
│ ── Init. pyc
│ ── Models. py
│ ── Models. pyc
│ ── Tests. py
│ ── Urls. py
│ ── Urls. pyc
│ ── Views. py
│ ── Views. pyc
── Demo0414_userauth
│ ── Init. py
│ ── Init. pyc
│ ── Settings. py
│ ── Settings. pyc
│ ── Urls. py
│ ── Urls. pyc
│ ── Wsgi. py
│ ── Wsgi. pyc
── Manage. py
└ ── Templates
── Register.html
├ ── Success.html
── Userlogin.html

4 directories, 29 files

Then add the app account to the installed_app of the setting file;

 

Create a templates folder, which can be placed in the project root directory or app directory. Generally, it is recommended to store them in the app directory. If you put down the root directory of the project, you must set 'dirs': [OS. path. join (BASE_DIR, 'templates')] In the setting file. Otherwise, the template cannot be used.

 

In addition, due to the page Jump problem in this project, in order to prevent csrf attacks safely, related settings are available in one template. At present, I still don't use this item. It is said that adding label {% csrf_token %} to form can be implemented, but I have not succeeded. So ignore this issue and comment out the middleware 'django. middleware. csrf. CsrfViewMiddleware 'in seeting.

 

Then, create the corresponding database in the model:

class User(models.Model): username = models.CharField(max_length=50) password = models.CharField(max_length=50) email = models.EmailField()

View. Pdb was used for breakpoint debugging. I like it very much. If you are not interested, just comment it out.

# Coding = utf-8from django. shortcuts import render, render_to_responsefrom django import formsfrom django. http import HttpResponse, HttpResponseRedirectfrom django. template import RequestContextfrom django. contrib import authfrom models import Userimport pdbdef login (request): if request. method = "POST": uf = UserFormLogin (request. POST) if uf. is_valid (): # obtain the form information username = uf. cleaned_data ['username'] Password = uf. cleaned_data ['Password'] userResult = User. objects. filter (username = username, password = password) # pdb. set_trace () if (len (userResult)> 0): return render_to_response('success.html ', {'operation': "login"}) else: return HttpResponse ("this user does not exist") else: uf = UserFormLogin () return render_to_response ("userlogin.html", {'U': uf}) def register (request): curtime = time. strftime ("% Y-% m-% d % H: % M: % S", time. loca Ltime (); if request. method = "POST": uf = UserForm (request. POST) if uf. is_valid (): # obtain the form information username = uf. cleaned_data ['username'] # pdb. set_trace () # try: filterResult = User. objects. filter (username = username) if len (filterResult)> 0: return render_to_response('register.html ', {"errors": "username already exists"}) else: password1 = uf. cleaned_data ['password1 '] password2 = uf. cleaned_data ['password2 '] errors = [] if (Password2! = Password1): errors. append ("the two passwords are inconsistent! ") Return render_to_response('register.html ', {'errors': errors}) # return HttpResponse ('the passwords entered twice are inconsistent !, Enter password again ') password = password2 email = uf. cleaned_data ['email '] # Write the form to the database user = User. objects. create (username = username, password = password1) # user = User (username = username, password = password, email = email) user. save () pdb. set_trace () # return to the registration success page return comment ', {'username': username, 'operation': "register"}) else: uf = UserForm () return render_to_response('register.html ', {'U': uf}) class UserForm (forms. form): username = forms. charField (label = 'username', max_length = 100) password1 = forms. charField (label = 'Password', widget = forms. passwordInput () password2 = forms. charField (label = 'Confirm password', widget = forms. passwordInput () email = forms. emailField (label = 'email ') class UserFormLogin (forms. form): username = forms. charField (label = 'username', max_length = 100) password = forms. charField (label = 'Password', widget = forms. passwordInput ())

The Tempaltes folder contains three pages:

Register.html

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

Userlogin.html

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

Success.html

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

Update database:

 

Running Server:

 

Registration page:

 

If you have not registered a user, click OK to go to the success page.

Logon page:

 

Click OK to go to the success page.

The Django user registration and logon tutorial is complete and I hope it will be helpful to you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.