通过使用Django的第三方库进行快速开发
安装软件
pip install django-simple-captcha
配置setting
使用python manage.py startapp user
应用后在setting.py中添加
自定义forms.py
在应用下创建forms.py并添加如下:
from django import forms
from captcha.fields import CaptchaField
class UserForm(forms.Form):
username = forms.CharField(label="用户名")
password = forms.CharField(label="密码",widget=forms.PasswordInput)
captcha = CaptchaField()
创建HTML文件
在与主目录同级的templates下
创建user.html并添加如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>陈十一yi-Document</title>
</head>
<body>
<p>用户名:{
{form.username}}</p>
<p>密 码:{
{form.password}}</p>
<p>验证码:{
{form.captcha}}</p>
</body>
</html>
定义视图
在应用下的views.py
中添加:
from django.shortcuts import render
from .forms import UserForm
# Create your views here.
def loginView(request):
if request.method=="GET":
form=UserForm
return render(request,'user.html',locals())
定义路由
在urls.py
中添加:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from user.views import loginView
urlpatterns = [
path('admin/', admin.site.urls),
path('captcha/', include('captcha.urls')),
# 图片验证码 路由
path('login/', loginView)
]
验证
使用python.exe .\manage.py runserver
启动项目登录web访问http://127.0.0.1:8000/login/
即可