[英]Styling of template tags in 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 %}
因此,問題是“當使用模板標簽顯示字段時如何設置注冊表樣式?
定義表單類時,可以通過以下方式添加css屬性:
username_field = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.