Django user registration and logon methods

Source: Internet
Author: User
Tags django tutorial
This article describes the Django user registration and logon methods in the Django tutorial, for more information about Django user registration and logon methods, see this article.

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;

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

  
  User registration Registration page:

Userlogin.html

  
  User registration Logon page:

Success.html

  
  

UPDATE Database:

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!

The above is the details of Django user registration and logon methods in the Django tutorial. For more information, see other related articles in the first PHP community!

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.