项目思维导图
配置数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dj5',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': 3306
}
}
STATIC_URL = '/static/'
STATICFILED_DIRS=[
os.path.join(BASE_DIR,'static')
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
day05 urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/',include('users.urls',namespace='user'))
]
users urls.py
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth.decorators import login_required
from users import views
urlpatterns = [
url(r'^register/',views.register,name='register'),
url(r'^login/',views.login,name='login'),
# 首页
url(r'^index/',login_required(views.index),name='index'),
# 注销
url(r'^logout',login_required(views.logout),name='logout')
]
users forms.py
'''Tangzhuzhu'''
'''
导包规则:
1.先引入python自带的
2.引入第三方
3.引入自定义
'''
from django import forms
from django.contrib.auth.models import User
class UserForm(forms.Form):
"""
"""
username = forms.CharField(required=True, max_length=5,min_length=2,
error_messages =
{'required':'用户名必填',
'max_lenghth':'用户名不能大于5个字符',
'min_lenght':'用户名不能少于2个字符'
})
psssword = forms.CharField(required=True,min_length=6,
error_messages={
'required':'密码必填',
'min_length':'密码不能少于六位'
})
password2 = forms.CharField(required=True,min_length=6,
error_messages={
'required':'密码必填',
'min_length':'密码不能少于六位'}
)
def clean(self):
# 校验用户名是否已经注册过
user = User.objects.filter(username=self.cleaned_data.get('username'))
if user:
# 如果用户存在
raise forms.ValidationError({'username':'用户名存在'})
# 校验密码是否相等
if self.cleaned_data.get('password') !=self.cleaned_data.get("password2"):
raise forms.ValidationError({"password":"两次密码不一致"})
class UserLoginForm(forms.Form):
username = forms.CharField(required=True, max_length=5,min_length=2,
error_messages =
{'required':'用户名必填',
'max_lenghth':'用户名不能大于5个字符',
'min_lenght':'用户名不能少于2个字符'
})
psssword = forms.CharField(required=True,min_length=6,
error_messages={
'required':'密码必填',
'min_length':'密码不能少于六位'
})
password2 = forms.CharField(required=True,min_length=6,
error_messages={
'required':'密码必填',
'min_length':'密码不能少于六位'}
)
def clean(self):
# 校验用户是否存在
user = User.objects.filter(username = self.cleaned_data['username'])
if not user:
raise forms.ValidationError({'username':'请先注册'})
return self.cleaned_data
view.py
from django.contrib.auth.models import User
from django.shortcuts import render
from users.forms import UserForm
from users.forms import UserLoginForm
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.contrib import auth
# Create your views here.
def register(request):
if request.method =="GET":
return render(request,'register.html')
if request.method == "POST":
# 校验页面中传递的参数是否填写完成
form = UserForm(request.post)
#is_valid():判断表单是否验证通过
if form.is_vaild():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
password2 =form.cleaned_data.get('password2')
User.objects.create_user(username=username, password=password)
return HttpResponseRedirect(reverse('user:login'))
else:
return render(request, 'register.html',{'form':form})
def login(request):
if request.method == "GET":
return render(request,'login.html')
if request.method == 'POST':
# 表单验证,用户名和密码是否填写,校验用户名是否注册
form = UserLoginForm(request.POST)
if form.is_valid():
# 校验用户名和密码与数据库中对应的是否匹配
user =auth.authenticate(username=form.cleaned_data['username'],
password=form.cleaned_data['password'])
if user:
# 用户名和密码是正确的
auth.login(request,user)
return HttpResponseRedirect(reverse('user:index'))
else:
# 密码不正确
return render(request,'login.html',{'error':'密码错误'})
else:
return render(request,'login.html',{'form':form})
def index(request):
if request.method =="GET":
return render(request,'index.html')
def logout(request):
if request.method == "GET":
# 注销
auth.logout(request)
return HttpResponseRedirect(reverse('user:login'))
register.html
{% extends 'base.html' %}
{% block title %}
{% endblock %}
{% block content %}
<form action="post">
{{ form.errors.username }}
<p>姓名:<input type="text",name="username"></p>
{{ form.errors.password }}
<p>密码:<input type="password",name="password"></p>
{{ form.errors.password2 }}
<p>确认密码:<input type="password"name="password2"></p>
<input type="submit" value="提交">
</form>
{% endblock %}
login.html
{% extends 'base.html'%}
{% block title %}
{% endblock %}
{% block content %}
<form action="post">
<input type="text",name="username">
<input type="password",name="password">
<input type="password",name="password2">
<p><a href="">注销</a></p>
</form>
{% ehdblock %}