1class ContactView(FormView):
2 form_class = ContactForm
3 template_name = 'contact-us.html'
4 success_url = reverse_lazy('<app_name>:contact-us')
5
6 def get_initial(self):
7 initial = super(ContactView, self).get_initial()
8 if self.request.user.is_authenticated:
9 initial.update({'name': self.request.user.get_full_name()})
10 return initial
11
12 def form_valid(self, form):
13 self.send_mail(form.cleaned_data)
14 return super(ContactView, self).form_valid(form)
15
16 def send_mail(self, valid_data):
17 # Send mail logic
18 print(valid_data)
19
1from django.http import HttpResponseRedirect
2from django.shortcuts import render
3
4from .forms import NameForm
5
6def get_name(request):
7 # if this is a POST request we need to process the form data
8 if request.method == 'POST':
9 # create a form instance and populate it with data from the request:
10 form = NameForm(request.POST)
11 # check whether it's valid:
12 if form.is_valid():
13 # process the data in form.cleaned_data as required
14 # ...
15 # redirect to a new URL:
16 return HttpResponseRedirect('/thanks/')
17
18 # if a GET (or any other method) we'll create a blank form
19 else:
20 form = NameForm()
21
22 return render(request, 'name.html', {'form': form})
23