我尝试使用以下代码来创建一个使用Django crispy_forms的表单,但是当它呈现时会为布局提供简单的html
forms.py
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
class SimpleForm(forms.Form):
username = forms.CharField(label="Username", required="True")
password = forms.CharField(label="Password", required="True", widget=forms.PasswordInput)
remember = forms.BooleanField(label="Remember Me !")
helper = FormHelper()
helper.form_method = 'POST'
helper.add_input(Submit('login', 'login', css_class='btn-primary'))
的index.html
{% load crispy_forms_tags %}
<html>
<head>
<link rel="stylesheet" type="text/css" href="/home/vish/demo/new_form/form_demo/templates/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
{% crispy form %}
</div>
</div>
</body>
</html>
我做错了什么?请提出必要的改进建议。
答案 0 :(得分:1)
您可以定义布局,例如:
self.helper.layout = layout.Layout(
Div(
Div(
Fieldset('',
Div(
Div(
'first_name',
css_class="col-sm-6",
),
Div(
'last_name',
css_class="col-sm-6",
),
css_class="row",
),
),
css_class='row'
),
)
Obs:我在这个例子中使用过bootstrap类,你应该做什么!