Django form入门详解--2

 

调整form的输出格式:html

  默认状况下form的格式化输出是基本table的样式的、可是django中仍是为form提供发别的输出样式django

  一、默认的table样式输出app

<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action={% url 'your-name' %} method="POST"> {% csrf_token %} {{ form }} <!-- 这里采用的是django的默认输出方式 -->
            <input type="submit" value="submit">
        </form>
    </body>
</html>
<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action=/app01/yourname method="POST">
            <input type='hidden' name='csrfmiddlewaretoken' value='gR8rgaARcg8F3GWX4igz8xE6EXTrdyr8QoVJpZVrG9Cc3ibgvz9FioIfPNTRUh59' /> 
            <tr><th><label for="id_your_name">Your name:</label></th><td><input type="text" name="your_name" maxlength="100" required id="id_your_name" /></td></tr>
            <input type="submit" value="submit">
        </form>
    </body>
</html>

  能够看到默认状况下输出内容以table中的行的方式输出学习

 

 

  二、把输出方式调整为pui

<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action={% url 'your-name' %} method="POST"> {% csrf_token %} <!-- 用 p 的方式格式化输入 --> {{ form.as_p }} <input type="submit" value="submit">
        </form>
    </body>
</html>
<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action=/app01/yourname method="POST">
            <input type='hidden' name='csrfmiddlewaretoken' value='rDlHnoLxu991YKhlb22qVuYbSDDfCHwX1a8Zwd67Y2DyYmwECjVw5l2k3tDFjqaY' /> 
            <!-- 用 p 的方式格式化输入 -->
            <p><label for="id_your_name">Your name:</label> <input type="text" name="your_name" maxlength="100" required id="id_your_name" /></p> 
            <input type="submit" value="submit">
        </form>
    </body>
</html>

 

  三、以列表的方式输出url

<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action={% url 'your-name' %} method="POST"> {% csrf_token %} <!-- 用 p 的方式格式化输入 --> {{ form.as_ul }} <input type="submit" value="submit">
        </form>
    </body>
</html>
<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action=/app01/yourname method="POST">
            <input type='hidden' name='csrfmiddlewaretoken' value='ZgjUKC9RLivh2aZKdHB3c2QOn4ZhwcMVzN6cTrurfbZO2Me3EYu9mTUXyUZHdVqW' /> 
            <!-- 用 p 的方式格式化输入 -->
            <li><label for="id_your_name">Your name:</label> <input type="text" name="your_name" maxlength="100" required id="id_your_name" /></li> 
            <input type="submit" value="submit">
        </form>
    </body>
</html>

 

对form输出格式的总结:spa

  一、{{ form }} 直接输出、这种默认的方式是以table的形式格式化的;code

  二、{{ form.as_p }} 以 p 段落的方式输出;orm

  三、{{ form.as_ul }} 以 ul 列表的方式输出;csrf

 

 

默认三种输出的不足:

  若是这三种django自带的输出方式知足不了你的需求、那么“老铁扎心啦!”、你可能要本身定义form的输出了、

  再这以前咱们还要学习更多的关于form的东西才行

 

  一、form.field_name:

<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action={% url 'your-name' %} method="POST"> {{ form.your_name }} </form>
    </body>
</html>

  能够看到模板中直接对form.field_name 进行打印、输出以下:

<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action=/app01/yourname method="POST">
            <input type="text" name="your_name" maxlength="100" required id="id_your_name" /> 
        </form>
    </body>
</html>

  扎心啦、一个字段就直接对应一个 input标签。

 

 

  二、form.field_name.lable:

<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action={% url 'your-name' %} method="POST"> {{ form.your_name.label}} </form>
    </body>
</html>

  生成的代码以下

<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action=/app01/yourname method="POST"> Your name </form>
    </body>
</html>

 

  三、form.field_name.label_tag:

<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action={% url 'your-name' %} method="POST"> {{ form.your_name.label_tag}} </form>
    </body>
</html>
<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action=/app01/yourname method="POST">
            <label for="id_your_name">Your name:</label> 
        </form>
    </body>
</html>

 

  四、form.field_name.id_for_label:

<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action={% url 'your-name' %} method="POST"> {{ form.your_name.id_for_label }} </form>
    </body>
</html>
<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action=/app01/yourname method="POST"> id_your_name </form>
    </body>
</html>

 

  五、form.field_name.value:

<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action={% url 'your-name' %} method="POST"> {{ form.your_name.value }} </form>
    </body>
</html>
<html>
    <head>
        <title>your name</title>
        <meta charset="utf8"/>
    </head>

    <body>
        <form action=/app01/yourname method="POST"> None </form>
    </body>
</html>

 

    

  

 

 

 

 

 

 

 

 

 

 

----

相关文章
相关标签/搜索