-
Notifications
You must be signed in to change notification settings - Fork 5
4.7 View
As said earlier, urls.py
and views.py
connects the back-end models.py
(and the database) with front-end template HTMLs, so that as we navigate through the website, we can get the information we need from the back-end and display them in the front-end web pages.
In Start With A Single Click, we already introduce the generic classes of ListView
and DetailView
. In this page, we will introduce other classes and function defined in views.py
.
This is easy to understand: Whenever we only want to display the content of the template, we can use Template View:
class AboutView(TemplateView):
template_name = "about.html"
We talked about this briefly in 4.6 Form:
# you cannot create a post unless logged in
login_url = '/login/'
redirect_field_name = 'blog/post_detail.html' # save the new post, and it redirects to post_detail page
form_class = PostForm # This creates a new PostForm, and PostForm already specifies which fields we need to create
model = Post
The CreatePostView
class not only inherits the generic class of CreateView
, but also LoginRequiredMixin
, which means that to create a post, it needs the user to log in, and login_url = '/login/'
is the link that we will direct the user to log in. redirect_field_name = 'blog/post_detail.html'
means when we click the save
button of the form, where should the website direct you to. form_class = PostForm
and model = Post will infer that not only the model is Post
, but also the object of the model is created using a form, which we talked about in detail in 4.6 Form.
After creating a post, the user might want to go back and update the post, which will user UpdateView
to achieve this goal. Similar with CreateView
, UpdateView
on this website looks like this:
class PostUpdateView(LoginRequiredMixin, UpdateView): # this is the same with creating one (the view)
login_url = '/login/'
redirect_field_name = 'blog/post_detail.html'
form_class = PostForm
model = Post
This is literally the same with PostCreateView
, the only difference is that we user UpdateView
rather than CreateView
, and UpdateView
is trigger by:
<a class="btn btn-primary" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
in post_detail.html
, which will refer to:
url(r'^post/(?P<pk>\d+)/edit/$', views.PostUpdateView.as_view(), name='post_edit'), # edit view
in urls.py
, and triggers PostUpdateView
, which refer to PostForm
class in Forms.py
and it also search for post_form.html
template to display the form for the user to update.
When the user wants to delete a post, it is done by DeleteView
:
class PostDeleteView(LoginRequiredMixin, DeleteView):
model = Post
success_url = reverse_lazy('post_list') # when manage to delete the post, go back to post_list view
which only specifies the model where you want to delete an object from, and also provide a link to direct after you manage to delete the post. It automatically searches for post_confirm_delete.html
page to display the content, and the process of how this view is triggered is the same with how CreateView
is triggered.