How can I write a url for a form using TemplateView. I wrote a method to validate and pass the company details through form. Using that form object that I'm trying to access the HTML fields.

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,['[email protected]'])
                except BadHeaderError:
                        return BadHeaderError
            return render(request,'astra/company_details.html',{'form':form})
        else:
            return render(request,'astra/company_details.html')

I want to run my company_details.html file using TemplateView. I'm not able to write the url for same. Plz suggest

1 Answers

2
mk6181 On

TemplateView only have get method

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

if you have get and post methods use FormView