使用django自定义模板标签
使用Django Form实现的功能包含一下几个图:
将blog_detail.html中的form标签粘贴到公共的templates/login.html中,
{% extends 'base.html' %}
{% load static %}
{% block title %}
我的网站|登录
{% endblock %}
{% block nav_home_active %}active{% endblock %}
{% block content %}
登录
{% endblock %}
重写mysite/views.py中的login函数(反向解析获取首页链接)
from .forms import LoginForm, RegForm
...
def login(request):
if request.method == 'POST':
login_form = LoginForm(request.POST)
if login_form.is_valid():
user = login_form.cleaned_data['user']
auth.login(request, user)
return redirect(request.GET.get('from', reverse('home')))
else:
login_form = LoginForm()
context = {}
context['login_form'] = login_form
return render(request, 'login.html', context)
在blog_detail.html中的登录前面加:您尚未登陆...
您尚未登录,登录之后方可评论~
登录
or
注册
然后使用Django Form:
在mysite中创建forms.py文件,然后创建loginForm函数
from django import forms
from django.contrib import auth
from django.contrib.auth.models import User
class LoginForm(forms.Form):
username = forms.CharField(label='用户名',
widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'请输入用户名'}))
password = forms.CharField(label='密码',
widget=forms.PasswordInput(attrs={'class':'form-control', 'placeholder':'请输入密码'}))
def clean(self):
username = self.cleaned_data['username']
password = self.cleaned_data['password']
user = auth.authenticate(username=username, password=password)
if user is None:
raise forms.ValidationError('用户名或密码不正确')
else:
self.cleaned_data['user'] = user
return self.cleaned_data
然后增加注册功能:
在forms.py中增加RegForm的类:
# forms.py
class RegForm(forms.Form):
"""
用户注册功能实现
"""
username = forms.CharField(label='用户名',
max_length=30,
min_length=3,
widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'请输入3-30位用户名'}))
email = forms.EmailField(label='邮箱',
widget=forms.EmailInput(attrs={'class':'form-control', 'placeholder':'请输入邮箱'}))
password = forms.CharField(label='密码',
min_length=6,
widget=forms.PasswordInput(attrs={'class':'form-control', 'placeholder':'请输入密码'}))
password_again = forms.CharField(label='再输入一次密码',
min_length=6,
widget=forms.PasswordInput(attrs={'class':'form-control', 'placeholder':'再输入一次密码'}))
def clean_username(self):
"""
针对用户名进行验证
"""
username = self.cleaned_data['username']
if User.objects.filter(username=username).exists():
raise forms.ValidationError('用户名已存在')
return username
def clean_email(self):
"""
针对用户邮箱进行验证
"""
email = self.cleaned_data['email']
if User.objects.filter(email=email).exists():
raise forms.ValidationError('邮箱已存在')
return email
def clean_password_again(self):
"""
验证再次输入的密码是否和上面第一次输入的密码一致
"""
password = self.cleaned_data['password']
password_again = self.cleaned_data['password_again']
if password != password_again:
raise forms.ValidationError('两次输入的密码不一致')
return password_again
在mysite/views.py中增加对注册功能的处理函数,和login函数基本一样
from .forms import LoginForm, RegForm
from django.contrib.auth.models import User
...
def register(request):
"""
注册功能的视图处理函数
"""
if request.method == 'POST':
reg_form = RegForm(request.POST)
if reg_form.is_valid():
"""
因为前端需要这些参数,所以这里先从后端提取,然后返回给前端
"""
username = reg_form.cleaned_data['username']
email = reg_form.cleaned_data['email']
password = reg_form.cleaned_data['password']
# 创建用户
user = User.objects.create_user(username, email, password)
user.save()
# 登录用户
user = auth.authenticate(username=username, password=password)
auth.login(request, user)
return redirect(request.GET.get('from', reverse('home')))
else:
reg_form = RegForm()
context = {}
context['reg_form'] = reg_form
return render(request, 'register.html', context)
在总的mysite/urls.py中增加register的路由匹配
path('register/', views.register, name='register'),
在公共的templates中新建register.html,内容和login.html一样,区别就是登录改成注册,然后增加几个标签即可:
{% extends 'base.html' %}
{% load static %}
{% block title %}
我的网站|注册
{% endblock %}
{% block nav_home_active %}active{% endblock %}
{% block content %}
注册
{% endblock %}