zoukankan      html  css  js  c++  java
  • Django基础之form表单的补充进阶

    1. 应用Bootstrap样式

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="x-ua-compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
      <title>login</title>
    </head>
    <body>
    <div class="container">
      <div class="row">
        <form action="/login2/" method="post" novalidate class="form-horizontal">
          {% csrf_token %}
          <div class="form-group">
            <label for="{{ form_obj.username.id_for_label }}"
                   class="col-md-2 control-label">{{ form_obj.username.label }}</label>
            <div class="col-md-10">
              {{ form_obj.username }}
              <span class="help-block">{{ form_obj.username.errors.0 }}</span>
            </div>
          </div>
          <div class="form-group">
            <label for="{{ form_obj.pwd.id_for_label }}" class="col-md-2 control-label">{{ form_obj.pwd.label }}</label>
            <div class="col-md-10">
              {{ form_obj.pwd }}
              <span class="help-block">{{ form_obj.pwd.errors.0 }}</span>
            </div>
          </div>
          <div class="form-group">
          <label class="col-md-2 control-label">{{ form_obj.gender.label }}</label>
            <div class="col-md-10">
              <div class="radio">
                {% for radio in form_obj.gender %}
                  <label for="{{ radio.id_for_label }}">
                    {{ radio.tag }}{{ radio.choice_label }}
                  </label>
                {% endfor %}
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
              <button type="submit" class="btn btn-default">注册</button>
            </div>
          </div>
        </form>
      </div>
    </div>
    
    <script src="/static/jquery-3.2.1.min.js"></script>
    <script src="/static/bootstrap/js/bootstrap.min.js"></script>
    </body>
    </html>

    2. 批量添加样式

    可通过重写form类的init方法来实现:

    class LoginForm(forms.Form):
        username = forms.CharField(
            min_length=8,
            label="用户名",
            initial="yang",
            error_messages={
                "required": "不能为空",
                "invalid": "格式错误",
                "min_length": "用户名最短8位"
            }
        )
        ...
        
        def __init__(self, *args, **kwargs):
            super(LoginForm, self).__init__(*args, **kwargs)
            for field in iter(self.fields):
                self.fields[field].widget.attrs.update(
                {"class": "form-control"}
                )
    

      

  • 相关阅读:
    mysql高级查询(转的)
    PHP Content-type 的说明
    在textFieldShouldEndEditing 方法中调用[self.tableView reloadData] 失效问题
    关于navigationcontroller 隐藏navigationBar问题
    iOS证书、签名、安装描述文件
    swift 项目搭建
    定义私有cocoapods 源
    Git 服务器搭建
    UITabelView 介绍
    swift aes 加密
  • 原文地址:https://www.cnblogs.com/yang-wei/p/10003686.html
Copyright © 2011-2022 走看看