在Django模板标签的样式

时间:2015-05-07 01:52:39

标签: html css django

我试图设置我的注册表格,但由于它使用模板标签在字段中合并以填写,我不知道该怎么做。

我希望我的注册表格看起来像我的登录表单。登录表单的代码如下:

{% extends 'rango/base.html' %}

{% load staticfiles %}

{% block body_block %}

<link href="http://getbootstrap.com/examples/signin/signin.css" rel="stylesheet">

<form class="form-signin" role="form" method="post" action=".">
{% csrf_token %}

<h2 class="form-signin-heading">Please sign in</h2>

<input class="form-control" placeholder="Username" id="id_username" maxlength="254" name="username" type="text" required autofocus=""/>
<input type="password" class="form-control" placeholder="Password" id="id_password" name="password" type="password" required />

        <button class="btn btn-lg btn-primary btn-block" type="submit" value="Submit" />Sign in</button>
        </form>

{% endblock %}

...我的注册表格代码如下:

{% extends 'rango/base.html' %}

{% block title %}Register{% endblock %}

{% block body_block %}

<link href="http://getbootstrap.com/examples/signin/signin.css" rel="stylesheet">

        <div class="container">
            {% if registered %}
            <p>Thank you for registering.</p>

            <p><a href="/rango/login/">Login</a> when you are ready to rango.</p>
            {% else %}
            <form class="form-signin span8" id="user_form" method="post" action="/rango/register/"
                enctype="multipart/form-data">
                {% csrf_token %}
                <h2 class="form-signin-heading">Sign up here</h2>
                <!-- Display each form here -->

                {% for field in user_form.visible_fields %}
                {{ field.errors }}
                {{ field.help_text }}<br />
                {{ field }}<br/>
                {% endfor %}

                {% for field in profile_form.visible_fields %}
                {{ field.errors }}
                {{ field.help_text }}<br />
                {{ field }}<br />
                {% endfor %}


                <br />
                <!-- Provide a button to click to submit the form. -->
                <button class="btn btn-primary" type="submit" name="submit">Register</button>
            </form>
            {% endif %}
        </div>
    </div>
{% endblock %}

所以问题是&#34;当我使用模板标签显示字段时,如何设置注册表单的样式?

1 个答案:

答案 0 :(得分:0)

定义表单类时,可以通过以下方式添加css属性:

username_field = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))