简体   繁体   中英

Styling of template tags in Django

I'm trying to style my register form but since it uses template tags to merge in the fields to fill in I'm not sure how to do it.

I want my register form to look like my login form. The code for the login form is here:

{% 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 %}

... and the code for my register form looks like this:

{% 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 %}

So the question is "How can I style the register form when it uses template tags to show the fields?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM