url
from django.contrib import admin from app01 import views from django.conf.urls import url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/',views.login) ]
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form method="POST" action="/login/"> {% csrf_token %} <p> 用户:<input type="text" name="username" />{{ obj.errors.username.0 }} </p> <p> 密码:<input type="password" name="password" />{{ obj.errors.password.0 }} </p> <input type="submit" value="提交"> </form> </body> </html>
view
from django.shortcuts import render,HttpResponse,redirect # Create your views here. from django.forms import Form from django.forms import fields class LoginForm(Form): #长度,是否为空的验证,正则验证:不能为空,长度6-18 username = fields.CharField(max_length=18, min_length=6, required=True, error_messages={ 'required':'用户名不能为空', 'max_length':'用户名太长', 'min_length':'用户名太短', } ) #长度,是否为空的验证,正则验证:不能为空,长度大于5 password = fields.CharField(min_length=5,required=True) def login(request): if request.method == "GET": return render(request,'login.html') else: obj = LoginForm(request.POST) if obj.is_valid(): print(obj.cleaned_data) ###字典类型 return redirect('https://www.baidu.com') else: return render(request,'login.html',{'obj':obj})