我是 Django 的新手。我尝试构建基于类的 View 以创建对象。
模板中表单的默认名称是 form,我想将其更改为 "ajoutersource"
但我不知道如何更改。
views.py
class ajoutSource(CreateView):
model = Source
template_name = "pmd/ajouterSource.html"
form_class = AjouterSourceForm
success_url = reverse_lazy(ListerSources)
ajouterSource.html
{% for field in ajoutersource %}
<div class="row">
{% if field.errors %}
<div class="error">{{ field.errors }}</div>
{% endif %}
<div class="label">{{ field.label }}</div>
<div class="field">{{ field }}</div>
</div>
{% endfor %}
最佳答案
覆盖 get_context_data()
:
class ajoutSource(CreateView):
model = Source
template_name = "pmd/ajouterSource.html"
form_class = AjouterSourceForm
success_url = reverse_lazy(ListerSources)
def get_context_data(self, **kwargs):
context = super(ajoutSource, self).get_context_data(**kwargs)
context["ajoutersource"]=context["form"]
return context
关于python - 基于 Django 类的 View : Override form name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33485805/