Django实现用户注册登录

学习Django中:写一个用户注册登录系统,开始搞事情 =====O(∩_∩)O~=====

=================

Ubuntu

python 2.7.12

Django 1.10.4

IDE:Pycharm

Bootstrap

=================

新建项目:(我是直接用pycharm直接生成的)

使用终端:

(创建项目) django-admin.py startproject mysite

(进入mysite新建app) django-admin.py startapp app01

Django实现用户注册登录_第1张图片

记得在settings.py里面添加app

Django实现用户注册登录_第2张图片

 设计模型:

/mysite/app01/models.py:

复制代码

 1 from __future__ import unicode_literals
 2 
 3 from django.db import models
 4 
 5 # Create your models here.
 6 
 7 class User(models.Model):
 8     username = models.CharField(max_length=50)
 9     password = models.CharField(max_length=50)
10     email = models.EmailField()

复制代码

创建User类,存放 username、password、email三个字段

同步数据库:

Python manage.py makemigrations

python manage.py migrate

Django会自动创建一系列表

Django实现用户注册登录_第3张图片

没有自动创建superuser.......咱们手动创建:

python manage.py createsuperuser

Django实现用户注册登录_第4张图片

设计逻辑视图(views):(使用表单)

/mysite/app01/views.py:

复制代码

 1 #coding=utf-8
 2 from django.shortcuts import render,render_to_response
 3 from django.http import HttpResponse
 4 from django import forms
 5 from models import User
 6 # Create your views here.
 7 class UserForm(forms.Form):
 8     username = forms.CharField(label='用户名',max_length=50)
 9     password = forms.CharField(label='密码',widget=forms.PasswordInput())
10     email = forms.EmailField(label='邮箱')
11 
12 def regist(request):
13     if request.method == 'POST':    
14         userform = UserForm(request.POST)
15         if userform.is_valid():
16             username = userform.cleaned_data['username']
17             password = userform.cleaned_data['password']
18             email = userform.cleaned_data['email']
19 
20             User.objects.create(username=username,password=password,email=email)
21             User.save()
22 
23             return HttpResponse('regist success!!!')
24     else:
25         userform = UserForm()
26     return render_to_response('regist.html',{'userform':userform})
27 
28 def login(request):
29     if request.method == 'POST':
30         userform = UserForm(request.POST)
31         if userform.is_valid():
32             username = userform.cleaned_data['username']
33             password = userform.cleaned_data['password']
34 
35             user = User.objects.filter(username__exact=username,password__exact=password)
36 
37             if user:
38                 return render_to_response('index.html',{'userform':userform})
39             else:
40                 return HttpResponse('用户名或密码错误,请重新登录')
41 
42     else:
43         userform = UserForm()
44     return render_to_response('login.html',{'userform':userform})

复制代码

 

注释:

label:标签

widget:装饰

widget=forms.PasswordInput():设置密码字段

设计模板文件

在templates里面新建index.html、regist.html、login.html

regist.html

复制代码


{% load static %}


    
    
    
    
    
    

    Regist

    
    

    
    

    
    

    
    
    

    
    
  


注册页面

{{userform.as_p}}

复制代码

 

 login.html

复制代码


{% load static %}


    
    
    
    
    
    

    Login

    
    

    
    

    
    

    
    
    

    
    
  


登录页面

{{userform.as_p}}

复制代码

 

 index.html

复制代码




    
    主页



Hello Word!

复制代码

 

 设计urls

/mysite/urls.py

复制代码

from django.conf.urls import url,include
from django.contrib import admin
from app01 import urls
import app01

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'',include(app01.urls)),
]

复制代码

 

 /mysite/app01/urls.py

复制代码

from django.conf.urls import url,include
from django.contrib import admin
import views
admin.autodiscover()

urlpatterns = [
    url(r'^index/$',views.index),
    url(r'^login/$',views.login),
    url(r'^regist/$',views.regist),

]

复制代码

 

 使用admin后台管理注册的用户

在models.py里面设计一个UserAdmin类,用来记录注册用户的信息

/mysite/app01/models.py

复制代码

from __future__ import unicode_literals
from django.contrib import admin
from django.db import models

# Create your models here.

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

class UserAdmin(admin.ModelAdmin):
    list_display = ('username','password','email')

admin.site.register(User,UserAdmin)

复制代码

 

 同步一下数据库(方法同上)

 效果图

主页:

Django实现用户注册登录_第5张图片

注册页:

Django实现用户注册登录_第6张图片

登录页面:

Django实现用户注册登录_第7张图片

 后台:

Django实现用户注册登录_第8张图片

Django实现用户注册登录_第9张图片

你可能感兴趣的:(python-web)