Django和crispy form,如何在crispy Layout中添加id和name
Django and crispy form, how to add id and name in the crispy Layout
我正在尝试在我的模板中使用 Crispy Forms,但我无法在布局中正确呈现 name
、id
和 class
。
换句话说,我有以下模板:
<div class="modal-body">
<label for="conto">Conto</label>
<input class="form-control" id="form-conto" name="formConto"/>
</div>
所以我想删除输入行并添加脆皮字段,以便输入字段上的id="form-conto" name="formConto"
。
我知道我必须在 Model.forms 中添加布局,但我不明白如何获得它。
这是我的表格:
class MaterialeForm(forms.ModelForm):
class Meta:
model = Materiale
fields = "__all__"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
这是我的模型:
class Materiale(models.Model):
conto = models.ForeignKey(Conto, on_delete=models.CASCADE, null=True)
在模板顶部,加载 crispy 标签:
{% load crispy_forms_tags %}
然后,告诉 Crispy 使用 Crispy 标签呈现您的表单:
<div class="modal-body">
{% crispy materialeform materialeform.helper %}
</div>
在您的 forms.py
中,您需要添加 Layout
:
from crispy_forms import FormHelper, Layout
...
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.layout = Layout(
Field('conta', id="form-conto", css_class="form-control", title="Conto")
)
查看有关布局的文档:https://django-crispy-forms.readthedocs.io/en/latest/layouts.html
然后,当对表单发出 GET 请求时,它将(或多或少)按照您的需要呈现。你可能需要调整一些东西。按照上面的布局文档到达那里。
但是 none 这将起作用,除非您实际在模板中传递表单。也许你已经在这样做了,例如,使用通用的 FormView
,但如果没有,下面是你在视图中需要的内容:
from .forms import MaterialeForm
from django.template import RequestContext
def materialeview(request, template_name):
materialeform = MaterialeForm()
# Form handling logic
[...]
return render_to_response(template_name, {'materialeform': materialeform}, context_instance=RequestContext(request))
在 RequestContext
上,参见 https://docs.djangoproject.com/en/3.0/ref/templates/api/#using-requestcontext。
有关 Crispy Forms 的更多信息,请参阅 https://django-crispy-forms.readthedocs.io/en/latest/crispy_tag_forms.html。
最后,由于 Crispy Forms 在后台执行了很多操作,您可以考虑通过大声告诉它失败来消除混淆。将其放入您的 settings.py
文件中:
CRISPY_FAIL_SILENTLY = not DEBUG
顺便说一句,如果您还不太了解 Django Forms,那么 Crispy Forms 可能会引起很多混淆。我会说首先从 Django 的内置 Forms 开始,然后当你想做更高级的东西时再变脆。此处的文档应该有所帮助:https://docs.djangoproject.com/en/3.0/topics/forms/.
我正在尝试在我的模板中使用 Crispy Forms,但我无法在布局中正确呈现 name
、id
和 class
。
换句话说,我有以下模板:
<div class="modal-body">
<label for="conto">Conto</label>
<input class="form-control" id="form-conto" name="formConto"/>
</div>
所以我想删除输入行并添加脆皮字段,以便输入字段上的id="form-conto" name="formConto"
。
我知道我必须在 Model.forms 中添加布局,但我不明白如何获得它。
这是我的表格:
class MaterialeForm(forms.ModelForm):
class Meta:
model = Materiale
fields = "__all__"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
这是我的模型:
class Materiale(models.Model):
conto = models.ForeignKey(Conto, on_delete=models.CASCADE, null=True)
在模板顶部,加载 crispy 标签:
{% load crispy_forms_tags %}
然后,告诉 Crispy 使用 Crispy 标签呈现您的表单:
<div class="modal-body">
{% crispy materialeform materialeform.helper %}
</div>
在您的 forms.py
中,您需要添加 Layout
:
from crispy_forms import FormHelper, Layout
...
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.layout = Layout(
Field('conta', id="form-conto", css_class="form-control", title="Conto")
)
查看有关布局的文档:https://django-crispy-forms.readthedocs.io/en/latest/layouts.html
然后,当对表单发出 GET 请求时,它将(或多或少)按照您的需要呈现。你可能需要调整一些东西。按照上面的布局文档到达那里。
但是 none 这将起作用,除非您实际在模板中传递表单。也许你已经在这样做了,例如,使用通用的 FormView
,但如果没有,下面是你在视图中需要的内容:
from .forms import MaterialeForm
from django.template import RequestContext
def materialeview(request, template_name):
materialeform = MaterialeForm()
# Form handling logic
[...]
return render_to_response(template_name, {'materialeform': materialeform}, context_instance=RequestContext(request))
在 RequestContext
上,参见 https://docs.djangoproject.com/en/3.0/ref/templates/api/#using-requestcontext。
有关 Crispy Forms 的更多信息,请参阅 https://django-crispy-forms.readthedocs.io/en/latest/crispy_tag_forms.html。
最后,由于 Crispy Forms 在后台执行了很多操作,您可以考虑通过大声告诉它失败来消除混淆。将其放入您的 settings.py
文件中:
CRISPY_FAIL_SILENTLY = not DEBUG
顺便说一句,如果您还不太了解 Django Forms,那么 Crispy Forms 可能会引起很多混淆。我会说首先从 Django 的内置 Forms 开始,然后当你想做更高级的东西时再变脆。此处的文档应该有所帮助:https://docs.djangoproject.com/en/3.0/topics/forms/.