django列表 表单的渲染
django列表 表单的渲染
雪地花露 发表于2年前
django列表 表单的渲染
  • 发表于 2年前
  • 阅读 9
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>搜索</title>

<style> body { background-color: #bbff88; margin: 0; } #header h2 { background-color: #EE872A; margin: 0; border: 0; padding: 0; text-align: center; } #container { background-color: #8EC172; } #content { display: inline-block; } ul { list-style-type: none; list-style: outside; } li { display: inline-block; } ul,li{ margin: 0; padding: 0; } </style>

</head>

<body> <div id="container"> <div id="header"><h2>查询页面</h2></div> <div id="content"> <ul> <li> <select style="font-size: large"> <option value="name">用户名</option> <option value="id">工号</option> </select> </li> <li> <form method="post">{% csrf_token %} <input style="font-size: large" type="text" name="user_name" placeholder="用户名/邮箱"> <input style="font-size: large" type="submit" value="搜索"> </form> </li> </ul> </div> </div> </body> </html>

forms文件中定义 class LoginForm(forms.Form): ''' 登录表单 ''' username = forms.CharField(required=True,widget=forms.TextInput(attrs={"placeholder": "Username", "required": "required"}),max_length=50,error_messages={'required': 'username is needed'}) password = forms.CharField(label='密码',widget=forms.PasswordInput(attrs={"placeholder": "Password", "required": "required"}),max_length=20,error_messages={"required": "password不能为空"}) def clean(self): # 用户名 try: username=self.cleaned_data['username'] except Exception as e: raise forms.ValidationError(u"yonghuming") return self.cleaned_data

views文件中 def LoGin(request): try: if request.method == 'POST': login_form = LoginForm(request.POST) if login_form.is_valid(): username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] #获取的表单数据与数据库进行比较 user = authenticate(username = username,password = password) if user is not None: login(request,user) #perm = request.user.get_all_permissions() #perm = list(perm) return render_to_response('index.html',context_instance=RequestContext(request)) else: return render_to_response('error.html',context_instance=RequestContext(request,{'names':username})) return redirect(request.POST.get('source_url')) else: return render_to_response('error.html',context_instance=RequestContext(request,{'reason':login_form.errors}))

    else:
        login_form = LoginForm()
except Exception as e:
    print e
return render_to_response('login.html',context_instance=RequestContext(request,{'login_form':login_form})) ######html中调用login_form的属性

html文件中

<body> {% if password_is_wrong %} <<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button> <h4>错误!</h4>用户名或密码错误 </div> {% endif %} <div class="heaeder"> <h1>Openvpn 登录页面</h1> </div> <div class="content"> <form action="{% url 'login' %}" method="post"> #####模板中引入url变量,需要在urls文件中定义别名(url(r'^login/',LoGin,name='login'),) {% csrf_token %} <ul> <li>{{ login_form.username }}</li><br> <div class="clear"> </div> <li>{{ login_form.password }}</li><br> <div class="clear"> </div> <li><input type="submit" value = "登录" /> </li> <div class="clear"> </div> </ul> </form> </div> <table border="1"> </table> </body>

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 6
博文 32
码字总数 34881
×
雪地花露
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: