Python 如何为templateView创建URL?
如何使用TemplateView为表单编写url。我编写了一个方法来验证并通过表单传递公司的详细信息。使用我试图访问HTML字段的表单对象 Form.py View.pyPython 如何为templateView创建URL?,python,django,Python,Django,如何使用TemplateView为表单编写url。我编写了一个方法来验证并通过表单传递公司的详细信息。使用我试图访问HTML字段的表单对象 Form.py View.py 我想使用TemplateView运行我的company_details.html文件。我无法为相同的内容编写url。请建议模板视图只有get方法 def get(self, request, *args, **kwargs): return render(request,self.template_name, {'fo
我想使用TemplateView运行我的company_details.html文件。我无法为相同的内容编写url。请建议模板视图只有get方法
def get(self, request, *args, **kwargs):
return render(request,self.template_name, {'form': self.form})
如果您有get和post方法,请使用FormView在URL.py中尝试了什么?为什么要使用模板视图而不是create视图来为您处理表单?from django.urls从django.urls导入路径从django.views导入包含从django.views.generic导入模板视图从。导入视图app_name='astra'urlpatterns=[path,TemplateView.as_viewtemplate_name='astra/index.html',name='home',path'contact',views.ContactView.as_view,path'company',views.GetCompanyView.as_view,]
class GetCompanyView(TemplateView):
template_name = "astra/company_details.html"
form = CompanyDetailsForm()
def get_context_data(self,**kwargs):
context = super().get_context_data(**kwargs)
context['form']=self.form
return context
def company_details(request):
if request.method =="POST":
form = CompanyDetailsForm(request.POST)
if form.is_valid():
company_name = form.cleaned_data['company_name']
contact_person = form.cleaned_data['contact_person']
email = form.cleaned_data['email']
employee_count = form.cleaned_data['employee_count']
mobile_number = form.cleaned_data['mobile_number']
try:
form.save()
send_mail(company_name,contact_person,email,employee_count,mobile_number,['salesastra500@gmail.com'])
except BadHeaderError:
return BadHeaderError
return render(request,'astra/company_details.html',{'form':form})
else:
return render(request,'astra/company_details.html')
def get(self, request, *args, **kwargs):
return render(request,self.template_name, {'form': self.form})