[英]Applying bootstrap to a django registration page
I have a django app where I am using authentication system with forms. 我有一个django应用,我在其中使用带有表单的身份验证系统。 I was following this tutorial to do it.
我正在按照本教程进行操作。
<div class="panel panel-default">
{% if registered %}
<strong>Thank you for registering!</strong>
<a href="/lms/">Return to the homepage.</a><br />
{% else %}
<strong><h3>Register here!</h3></strong><br />
<form id="user_form" method="post" action="/lms/register/" enctype="multipart/form-data">
{% csrf_token %}
{{ user_form.as_p }}
{{ profile_form.as_p }}
<input type="submit" name="submit" class="btn btn-primary" value="Register" />
</form>
{% endif %}
</div>
Now I am trying to add these forms in bootstrap labels. 现在,我尝试将这些表单添加到引导标签中。 But since it is bringing all the elements of the forms in a single statement, how do I make these labels to show up using bootstrap?
但是由于将表单的所有元素都放在一个语句中,如何使用引导程序使这些标签显示出来?
you can use specific fields name instead of using .as_p . 您可以使用特定的字段名称而不是.as_p 。 For example let's assume that you have username and age field.
例如,假设您具有用户名和年龄字段。 Hence it will be like this
因此它将是这样
{{ user_form.username }}
{{ user_form.age }}
Hope it helped :) 希望它有帮助:)
You don't have to output the form in the template in a single statement. 您不必在单个语句中输出模板中的表单。 You could do something like:
您可以执行以下操作:
{% for field in user_form %}
<p class="some-bootstrapclass">{{ field.label_tag }}{{ field }}</p>
{% endfor %}
This is very simplified example. 这是非常简化的示例。
However I would strongly suggest you to take a look at crispy forms: 但是,我强烈建议您看一下脆皮形式:
http://django-crispy-forms.readthedocs.org http://django-crispy-forms.readthedocs.org
With crispy forms you can set everything in your forms.py and still output the forms in a single statement in the template, for example: 使用松脆的表单,您可以在forms.py中设置所有内容,并且仍在模板的单个语句中输出表单,例如:
{% load crispy_forms_tags %}
{% crispy user_form %}
The answers here are very good, and I've had success with Django Crispy forms (recommended in Two Scoops ), and other libraries like django-bootstrap
. 这里的答案非常好,我在Django Crispy表单(在Two Scoops中推荐)和其他类似
django-bootstrap
库中都取得了成功。 But in case another option is desired, I use forms to tweak the fields: 但是,如果需要其他选择,我可以使用表格来调整字段:
class UserAuthenticationForm(LoginForm):
username = forms.CharField(
max_length=30,
widget=forms.TextInput(
attrs={
'class': 'form-control',
'name': 'username',
'placeholder': 'Username',
'autofocus': 'autofocus',
}
)
)
Build out all the fields you want. 建立您想要的所有字段。 This gives finer control over exactly what you want the forms to look like.
这样可以更好地控制您想要的表单外观。 Then, for the template:
然后,对于模板:
<form class="login" method="post" action="{% url 'account_login' %}">
{% csrf_token %}
{{ form }}
. . .
{# add errors, etc... #}
<button class="btn btn-lg btn-primary" type="submit">
{% trans "Log in" %}
</button>
</form>
You can also override the views, if needed... 如果需要,您还可以覆盖视图...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.