注册、登入模块需要用到Django中内置的表单框架 官方文档地址
Django的表单功能可以简化和自动化上述工作的大部分内容,并且也能比大多数程序员自己编写代码去实现来的更安全些。
Django会处理涉及表单的三个不同部分:
- 准备并重组数据,以便下一步的渲染
- 为数据创建HTML 表单
- 接收并处理客户端提交的表单及数据
实战
在app下新建一个form来构建表单所需要的表单信息
基础跑通
- form.py
继承 forms.Form
from django import forms
class UserFrom(forms.Form):
user = forms.CharField()
pwd = forms.CharField()
- view.py
def regis(request):
# 如果这是一个POST请求,我们需要处理表单数据
if request.method == 'POST':
# 创建一个表单实例,并用来自请求的数据填充它:
form = UserFrom(request.POST)
# 检查是否有效:
if form.is_valid():
# 以格式处理数据。cleaned_data获取数据
user = form.cleaned_data['usre']
pwd = form.cleaned_data['pwd']
# redirect to a new URL:
return render(request,'login.html',locals())
# 如果使用GET(或任何其他方法),我们将创建一个空白表单
else:
form = UserFrom()
return render(request, 'login.html', {'form': form})
- login.html
<form action="/login/" method="post">
{{ form }}
<button type="submit">注册</button>
</form>
进阶(一)
form有许多属性---->文档地址
- form.py
PasswordInput : 相当于 input 的type=password,会隐藏输入信息
EmailInput:自动验证输入信息,必须为XXXXXX@XX.XXX邮箱形式。
from django import forms
class UserFrom(forms.Form):
user = forms.CharField(label="用户名",max_length=10,widget=forms.TextInput(attrs={'class':'u'}))
pwd = forms.CharField(label="密码",max_length=10,widget=forms.PasswordInput(attrs={'class':'p'}))
email = forms.CharField(label="邮箱",max_length=20,widget=forms.EmailInput(attrs={"class":'e'}))
- login.html
<form action="/login/" method="post">
<p>{{ form.user.label }}:{{ form.user }}</p>
<p>{{ form.pwd.label }}:{{ form.pwd }}</p>
<p>{{ form.email.label }}:{{ form.email }}</p>
<button type="submit">注册</button>
</form>
自动生成的前端代码
<form action="/login/" method="post">
<p>用户名:<input type="text" name="user" value="tom" class="u" maxlength="10" required="" id="id_user"></p>
<p>密码:<input type="password" name="pwd" class="p" maxlength="10" required="" id="id_pwd"></p>
<p>邮箱:<input type="email" name="email" class="e" maxlength="20" required="" id="id_email"></p>
<button type="submit">注册</button>
</form>
进阶(二)–加入验证信息、提示信息等
- form.py
from django import forms
class UserFrom(forms.Form):
user = forms.CharField(label="用户名",max_length=10,widget=forms.TextInput(attrs={'class':'u'}))
pwd1 = forms.CharField(label="密码",max_length=10,widget=forms.PasswordInput(attrs={'class':'p'}))
pwd2 = forms.CharField(label="重复密码", max_length=10, widget=forms.PasswordInput(attrs={'class': 'p'}))
email = forms.CharField(label="邮箱",max_length=20,widget=forms.EmailInput(attrs={"class":'e'}))
- view.py
加入判断信息,如果pwd1不等于pwd2,返回提示信息。
def regis(request):
if request.method == 'POST':
form = UserFrom(request.POST)
if form.is_valid():
user = form.cleaned_data['user']
pwd1 = form.cleaned_data['pwd1']
pwd2 = form.cleaned_data['pwd2']
if pwd1!=pwd2:
message = "密码不一样"
return render(request,'login.html',locals())
return render(request,'login.html',locals())
else:
message="信息不全"
return render(request,'login.html',locals())
else:
form = UserFrom()
return render(request, 'login.html', {'form': form})
- login.html
<form action="/login/" method="post">
{% if message %}
{{ message }}
{% endif %}
<p>{{ form.user.label }}:{{ form.user }}</p>
<p>{{ form.pwd1.label }}:{{ form.pwd1 }}</p>
<p>{{ form.pwd2.label }}:{{ form.pwd2 }}</p>
<p>{{ form.email.label }}:{{ form.email }}</p>
<button type="submit">注册</button>
</form>
输入密码不相同的时候,返回提示信息。
进阶(三)—验证码captcha
Django内置验证码
安装 pip install django-simple-captcha
在setting中注册
INSTALLED_APPS = [
~ ~ ~ ~ ~ ~ ~
'captcha'
]
- form.py
from django import forms
class UserFrom(forms.Form):
user = forms.CharField(label="用户名",max_length=10,widget=forms.TextInput(attrs={'class':'u'}))
pwd1 = forms.CharField(label="密码",max_length=10,widget=forms.PasswordInput(attrs={'class':'p'}))
pwd2 = forms.CharField(label="重复密码", max_length=10, widget=forms.PasswordInput(attrs={'class': 'p'}))
email = forms.CharField(label="邮箱",max_length=20,widget=forms.EmailInput(attrs={"class":'e'}))
captcha = CaptchaField(required=True,error_messages={"message":"验证码错误"})
- login.html
<form action="/login/" method="post">
{% if message %}
{{ message }}
{% endif %}
<p>{{ form.user.label }}:{{ form.user }}</p>
<p>{{ form.pwd1.label }}:{{ form.pwd1 }}</p>
<p>{{ form.pwd2.label }}:{{ form.pwd2 }}</p>
<p>{{ form.email.label }}:{{ form.email }}</p>
<p>{{ form.captcha }}</p>
<button type="submit">注册</button>
</form>
进阶(四)----session
- views.py
def regis(request):
if request.method == 'POST':
form = UserFrom(request.POST)
if form.is_valid():
user = form.cleaned_data['user']
pwd1 = form.cleaned_data['pwd1']
pwd2 = form.cleaned_data['pwd2']
if pwd1!=pwd2:
message = "密码不一样"
return render(request,'login.html',locals())
request.session['name'] = user
request.session['is_login'] = True
return render(request,'login.html',locals())
else:
message="信息不全"
return render(request,'login.html',locals())
else:
form = UserFrom()
return render(request, 'login.html', {'form': form})
- login.html
<form action="/login/" method="post">
{% if message %}
{{ message }}
{% endif %}
{% if request.session.is_login %}
欢迎你 {{ request.session.name }}
{% endif %}
<p>{{ form.user.label }}:{{ form.user }}</p>
<p>{{ form.pwd1.label }}:{{ form.pwd1 }}</p>
<p>{{ form.pwd2.label }}:{{ form.pwd2 }}</p>
<p>{{ form.email.label }}:{{ form.email }}</p>
<p>{{ form.captcha }}</p>
<button type="submit">注册</button>
</form>
登入成功后,判断session中的is_login是否为True,并返回前端页面
进阶(五)-----注销
urls.py
urlpatterns = [
path('logout/',logout)
]
- views.py
def logout(request):
request.session.flush()
form = UserFrom()
return render(request,'login.html',locals())
- login.py
添加
<a href="/logout/">注销</a>
点击注销后注销成功并返回注册页。
进阶(六)—注册
- models.py
class User(models.Model):
user = models.CharField(primary_key=True,max_length=20,unique=True)
password = models.CharField(max_length=20,null=False)
email = models.CharField(max_length=50,null=False)
def __str__(self):
return self.user
- views.py
def regis(request):
if request.method == 'POST':
form = UserFrom(request.POST)
if form.is_valid():
user = form.cleaned_data['user']
pwd1 = form.cleaned_data['pwd1']
pwd2 = form.cleaned_data['pwd2']
email = form.cleaned_data['email']
if pwd1!=pwd2:
message = "密码不一样"
return render(request,'login.html',locals())
request.session['name'] = user
request.session['is_login'] = True
new_user = User.objects.create()
new_user.user = user
new_user.password = pwd1
new_user.email=email
new_user.save()
return render(request,'login.html',locals())
else:
message="信息不全"
return render(request,'login.html',locals())
else:
form = UserFrom()
user_info = User.objects.all()
return render(request, 'login.html', {'form': form,'user_info':user_info})
- 验证
在控制台中输入
python manage.py shell
>>> from login.models import User
>>> User.objects.all()
<QuerySet [<User: tom>]>
>>> User.objects.all()
<QuerySet [<User: tom>, <User: jk>]>
>>>