Django Class 基于视图:覆盖表单名称
Django Class Based Views : Override form name
我是 Django 的新手。我尝试构建一个基于 class 的视图以创建一个对象。
模板中表单的默认名称是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 %}
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
按照下面的方法就可以了
方法一(模型形式)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['new_name'] = self.get_form()
return context
方法 2(简单形式)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['new_name'] = context["form"]
return context
推荐方法一
(注意:这是 python 3.6+ 语法,更改 super() 调用 python 2.0+)
覆盖 get_context_data
,context['form']
取自 SomeForm
,并更改为 form_1
,您可以在模板中使用 form_1
class Something(generic.CreateView):
template_name = 'app/example.html'
form_class = forms.SomeForm
model = models.SomeModel
def get_context_data(self, **kwargs):
context = super(Something, self).get_context_data(**kwargs)
context["form_1"] = context["form"]
context["form_2"] = forms.SomeForm2(**self.get_form_kwargs())
return context
我是 Django 的新手。我尝试构建一个基于 class 的视图以创建一个对象。
模板中表单的默认名称是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 %}
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
按照下面的方法就可以了
方法一(模型形式)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['new_name'] = self.get_form()
return context
方法 2(简单形式)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['new_name'] = context["form"]
return context
推荐方法一 (注意:这是 python 3.6+ 语法,更改 super() 调用 python 2.0+)
覆盖 get_context_data
,context['form']
取自 SomeForm
,并更改为 form_1
,您可以在模板中使用 form_1
class Something(generic.CreateView):
template_name = 'app/example.html'
form_class = forms.SomeForm
model = models.SomeModel
def get_context_data(self, **kwargs):
context = super(Something, self).get_context_data(**kwargs)
context["form_1"] = context["form"]
context["form_2"] = forms.SomeForm2(**self.get_form_kwargs())
return context