gpt4 book ai didi

python - 基于 Django 类的 View : Override form name

转载 作者:行者123 更新时间:2023-12-04 12:51:54 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com