将bootstrap应用于django注册页面(Applying bootstrap to a django registration page)

我有一个django应用程序,我正在使用带有表单的身份验证系统。 我正在按照本教程进行操作。

<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>

现在我试图在bootstrap标签中添加这些表单。 但由于它将表单的所有元素都放在一个语句中,如何使用bootstrap使这些标签显示出来?

I have a django app where I am using authentication system with forms. 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?

最满意答案

您不必在单个语句中输出模板中的表单。 你可以这样做:

{% for field in user_form %} <p class="some-bootstrapclass">{{ field.label_tag }}{{ field }}</p> {% endfor %}

这是一个非常简化的例子。

不过我强烈建议你看看脆皮形式:

http://django-crispy-forms.readthedocs.org

使用crispy表单,您可以在forms.py中设置所有内容,并仍然在模板中的单个语句中输出表单,例如:

{% load crispy_forms_tags %} {% crispy user_form %}

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

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:

{% load crispy_forms_tags %} {% crispy user_form %}

更多推荐