一、保存用户输入内容
如果用户输入一张表单提交后出现错误时,会出现重现填写的情况。我们可以把用户输入的信息保存下来,并返回到前台页面,这样用户就无需再次输入。
views.py
1 # coding:utf-8 2 from django.shortcuts import render, HttpResponse, redirect 3 from app01.forms import account as Account_Forms 4 5 6 def login(request): 7 8 obj = Account_Forms.UserInfo(request.POST) #将用户输入的内容添加到form的对象中 9 if request.method == ‘POST‘: 10 return render(request, ‘account/login.html‘, {‘obj‘: obj}) #将用户输入的内容返回到前台页面 11 return render(request, ‘account/login.html‘, {‘obj‘: obj})
forms.py
1 from django import forms 2 3 4 class UserInfo(forms.Form): 5 username = forms.CharField() 6 password = forms.CharField(widget=forms.PasswordInput())
html文件
1 {% load staticfiles %} 2 <!DOCTYPE html> 3 <html lang="en"> 4 <head> 5 <meta charset="UTF-8"> 6 <title></title> 7 </head> 8 <body> 9 10 <form action="/login/" method="post"> 11 <p>{{ obj.username }}</p> 12 <p>{{ obj.password }}</p> 13 <input type="submit" value="submit"> 14 </form> 15 16 {#<script type="text/javascript" src="{{ STATIC_URL }}/js/jquery-2.1.4.min.js">#} 17 <script type="text/javascript" src="{% static "js/jquery-2.1.4.min.js" %}"></script> 18 </body> 19 </html>
时间: 2024-10-27 18:42:28