Class based views with forms

36 views
Skip to first unread message

alex smolyakov

unread,
Apr 26, 2022, 10:04:19 AM4/26/22
to Django users
Can anyone explain me how to make send email form on page.

So my forms.py

class ContactForm(forms.Form): 
    name = forms.CharField(required=True)
    last_name = forms.CharField()
    adults = forms.IntegerField() children = forms.IntegerField() 
    email = forms.EmailField(required=True)
    message = forms.CharField(widget=forms.Textarea)

views.py

class TourDetailView(DetailView):
    model = models.Tour 
    template_name = 'tours/tour-detail.html' 
    form_class = forms.ContactForm
    success_url = 'tours.html'

As far as i understand i need to get data from request , but i dont know how to do it. There are a lot of information in function based views, but i didnt get how to do it in class based views.

And it would be nice of you to explain where should i put send_mail function. Any help will be appreciated.

Ryan Nowakowski

unread,
Apr 28, 2022, 1:31:21 PM4/28/22
to alex smolyakov, Django users
You're right, it's not very clear how to have a single class-based view
handle both displaying details and submitting a form.  This is a
fantastic site that shows how each class based view is set up
inheritance-wise: https://ccbv.co.uk/

Take a look the ancestors for DetailView:
https://ccbv.co.uk/projects/Django/4.0/django.views.generic.detail/DetailView/

... and the ancestors for FormView:
https://ccbv.co.uk/projects/Django/4.0/django.views.generic.edit/FormView/


Maybe you can just add the FormMixin as an ancestor to your DetailView.


As another option, maybe you can create a separate FormView that you can
POST to from your DetailView.


- Ryan N

Vishesh Mangla

unread,
Apr 28, 2022, 2:39:59 PM4/28/22
to django...@googlegroups.com
Iommi forms

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/235da153-a9d7-2582-ace4-3dbf147d17f8%40fattuba.com.
Reply all
Reply to author
Forward
0 new messages