1from django.urls import path
2from django.views.generic import TemplateView
3
4urlpatterns = [
5 path('about/', TemplateView.as_view(template_name="about.html")),
6]
7
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
1from myapp.forms import ContactForm
2from django.views.generic.edit import FormView
3
4class ContactView(FormView):
5 template_name = 'contact.html'
6 form_class = ContactForm
7 success_url = '/thanks/'
8
9 def form_valid(self, form):
10 # This method is called when valid form data has been POSTed.
11 # It should return an HttpResponse.
12 form.send_email()
13 return super().form_valid(form)
14
1###### views.py #####
2from .models import Article
3from .forms import UpdateArticleForm
4from django.views.generic import UpdateView
5class ArticleUpdateView(UpdateView):
6 model = Article
7 form_class = UpdateArticleForm
8 template_name = 'articles/create_article.html'
9
10###### urls.py ######
11from .views import ArticleUpdateView
12urlpatterns =[
13 path('articles/<int:pk>/update/', ArticleUpdateView.as_view()),]
14# pk is default value (for primary key of id of an instance of object