Introduction
In previous articles, we have learnt about Django, how it works and how we can use it in conjunction with Python to build several apps like a two paged web application, a message board app and a blog app. In the previous article, we learnt and saw steps on how to create and implement a blog app. This article is a continuation of the previous one as in this article, we shall further extend the features of our blog app by adding the creation of forms where users can create, edit and delete any of their blog entries. The question, therein is “What Are Forms?”.
Forms
Forms are structured documents used to collect information in a logical and meaningful fashion for communication and for passage to another entity. Forms are very common but very complicated to implement correctly. When accepting user input, proper error handling is required, there are also several security concerns (XSS Attacks) and UI considerations around how to alert the user to problems with the form, not to mention the need for redirects on success.
Fortunately, Django provides an easy and effective way to go about this. The Django’s built-in Forms abstract away much of the difficulty and provides a rich set of tools to handle common use cases while working with forms.
The first step is to open up our blog app directory in our visual studio code, then open the “base.html” file in the templates folder. There, we will update our base template to display a link to a page for entering our new blog posts by adding the following:
<a href= “{% url ‘post_new’ %}”></a>
where “post_new” is the name for our URL.
Your updated file should look like this:
templates/base.html
{% load static %}
<html>
<head>
<title>Django blog</title>
<link href=”https://fonts.googleapis.com/css?family=Source+Sans+Pro:400″
rel=”stylesheet”>
<link href=”{% static ‘css/base.css’ %}” rel=”stylesheet”>
</head>
<body>
<div>
<header>
<div class=”nav-left”>
<h1><a href=”{% url ‘home’ %}”>Django blog</a></h1>
</div>
<div class=”nav-right”>
<a href=”{% url ‘post_new’ %}”>+ New Blog Post</a>
</div>
</header>
{% block content %}
{% endblock content %}
</div>
</body>
</html>
blog/urls.py
Next, we need to update our urls.py file by importing our not yet created view called BlogCreateView and then adding a new URLConf for post_new. To do this, we do the following:
from django.urls import path
from .views import BlogListView, BlogDetailView, BlogCreateView #new
urlpatterns = [
path(‘post/new/’, BlogCreateView.as_view(), name=’post_new’), # new
path(‘post/<int:pk>/’, BlogDetailView.as_view(), name=’post_detail’),
path(”, BlogListView.as_view(), name=’home’),
]
blog/views.py
In the views.py file, import a new generic class called CreateView at the top and then subclass it to create a new view called BlogCreateView. To implement this, do the following:
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView # new
from .models import Post
class BlogListView(ListView):
model = Post
template_name = ‘home.html’
class BlogDetailView(DetailView):
model = Post
template_name = ‘post_detail.html’
class BlogCreateView(CreateView): # new
model = Post
template_name = ‘post_new.html’
fields = [‘title’, ‘author’, ‘body’]