Python Django表单字段不显示
我想在视图中显示简单的搜索表单 forms.py:Python Django表单字段不显示,python,django,django-forms,django-views,django-templates,Python,Django,Django Forms,Django Views,Django Templates,我想在视图中显示简单的搜索表单 forms.py: from django import forms from django.utils.translation import gettext_lazy as _ class Vehicle_Search_by_VIN(forms.Form): vin = models.CharField(max_length=17) first_registration_date = models.DateField() class Vehi
from django import forms
from django.utils.translation import gettext_lazy as _
class Vehicle_Search_by_VIN(forms.Form):
vin = models.CharField(max_length=17)
first_registration_date = models.DateField()
class Vehicle_Search_by_Plate(forms.Form):
plate = models.CharField(max_length=7)
last_four_diggits_of_vin = models.DateField(max_length=4)
from django.shortcuts import render
from django.views import View
from .forms import *
class VehicleSearch(View):
template = 'vehicle_search_template.html'
cxt = {
'Search_by_VIN': Vehicle_Search_by_VIN(),
'Search_by_Plate': Vehicle_Search_by_Plate()
}
def get(self, request):
return render(request, self.template, self.cxt)
<form class="by_vin" method="POST" action="">
{% csrf_token %}
{{ Search_by_VIN.as_p }}
<button name='action' value='login' type="submit">Suchen</button>
</form>
<form class="by_plate" method="POST" action="">
{% csrf_token %}
{{ Search_by_Plate.as_p }}
<button name='action' value='signup' type="submit">Suchen</button>
</form>
视图。py:
from django import forms
from django.utils.translation import gettext_lazy as _
class Vehicle_Search_by_VIN(forms.Form):
vin = models.CharField(max_length=17)
first_registration_date = models.DateField()
class Vehicle_Search_by_Plate(forms.Form):
plate = models.CharField(max_length=7)
last_four_diggits_of_vin = models.DateField(max_length=4)
from django.shortcuts import render
from django.views import View
from .forms import *
class VehicleSearch(View):
template = 'vehicle_search_template.html'
cxt = {
'Search_by_VIN': Vehicle_Search_by_VIN(),
'Search_by_Plate': Vehicle_Search_by_Plate()
}
def get(self, request):
return render(request, self.template, self.cxt)
<form class="by_vin" method="POST" action="">
{% csrf_token %}
{{ Search_by_VIN.as_p }}
<button name='action' value='login' type="submit">Suchen</button>
</form>
<form class="by_plate" method="POST" action="">
{% csrf_token %}
{{ Search_by_Plate.as_p }}
<button name='action' value='signup' type="submit">Suchen</button>
</form>
我的模板文件:
from django import forms
from django.utils.translation import gettext_lazy as _
class Vehicle_Search_by_VIN(forms.Form):
vin = models.CharField(max_length=17)
first_registration_date = models.DateField()
class Vehicle_Search_by_Plate(forms.Form):
plate = models.CharField(max_length=7)
last_four_diggits_of_vin = models.DateField(max_length=4)
from django.shortcuts import render
from django.views import View
from .forms import *
class VehicleSearch(View):
template = 'vehicle_search_template.html'
cxt = {
'Search_by_VIN': Vehicle_Search_by_VIN(),
'Search_by_Plate': Vehicle_Search_by_Plate()
}
def get(self, request):
return render(request, self.template, self.cxt)
<form class="by_vin" method="POST" action="">
{% csrf_token %}
{{ Search_by_VIN.as_p }}
<button name='action' value='login' type="submit">Suchen</button>
</form>
<form class="by_plate" method="POST" action="">
{% csrf_token %}
{{ Search_by_Plate.as_p }}
<button name='action' value='signup' type="submit">Suchen</button>
</form>
{%csrf_令牌%}
{{Search_by_VIN.as_p}
苏晨
{%csrf_令牌%}
{{Search_by_Plate.as_p}
苏晨
但因此,视图中仅显示提交按钮。有人知道为什么没有呈现我的表单吗?在
视图.py
中,我想您在get()
函数中缺少了*args
和**kwargs
参数
class VehicleSearch(View):
template = 'vehicle_search_template.html'
cxt = {
'Search_by_VIN': Vehicle_Search_by_VIN(),
'Search_by_Plate': Vehicle_Search_by_Plate()
}
def get(self, request, *args, **kwargs): # HERE
return render(request, self.template, self.cxt)
更新
默认情况下,基于类的视图只支持每个视图的单个表单,但根据您的逻辑,您可以使用很少的选项来克服此限制。在
views.py
中参考此线程,我认为您在get()
函数中缺少*args
和**kwargs
参数
class VehicleSearch(View):
template = 'vehicle_search_template.html'
cxt = {
'Search_by_VIN': Vehicle_Search_by_VIN(),
'Search_by_Plate': Vehicle_Search_by_Plate()
}
def get(self, request, *args, **kwargs): # HERE
return render(request, self.template, self.cxt)
更新
默认情况下,基于类的视图只支持每个视图的单个表单,但根据您的逻辑,您可以使用很少的选项来克服此限制。参考此线程尝试在模板变量中提供完整路径,例如,如果你的应用程序名为my_app,则
模板='my app/vehicle_search_template.html'
尝试在模板变量中提供完整路径,例如,如果你的应用程序名为my_app,则模板='myapp/vehicle\u search\u template.html'
不幸的是,这没有帮助:(不幸的是,这没有帮助:(这回答了你的问题吗?这回答了你的问题吗?