如何为templateView创建URL?

时间:2019-05-10 06:31:18

标签: python django

如何使用TemplateView为表单写一个URL。我编写了一种方法来验证公司的详细信息并将其通过form传递。使用我尝试访问HTML字段的form对象。

Form.py

class CompanyDetailsForm(forms.Form):
    class meta:
        fields = ['company_name','contact_person','employee_count','email','mobile_number']
        widgets = {
            'comment':Textarea(attrs={'cols':30,'rows':5}),
        }

    company_name = forms.CharField(max_length=100,widget=forms.TextInput(attrs={'placeholder':'company Name'}))
    contact_person = forms.CharField(max_length=100,widget=forms.TextInput(attrs={'placeholder':'Contact Person'})) 
    email = forms.CharField(max_length=100,widget=forms.TextInput(attrs={'placeholder':'Email'}))
    employee_count = forms.CharField(max_length=100,widget=forms.TextInput(attrs={'placeholder':'Number Of Employee'}))
    mobile_number = forms.CharField(max_length=100,widget=forms.TextInput(attrs={'placeholder':'Mobile Number'}))  

View.py

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')

我想使用company_details.html运行我的TemplateView文件。我无法写同样的网址。 Plz建议

1 个答案:

答案 0 :(得分:2)

TemplateView仅具有get方法

def get(self, request, *args, **kwargs):
    return render(request,self.template_name, {'form': self.form})

如果您有get和post方法,请使用FormView