Writing Your First Blog App In Django-Part19

We will continue to build our blog app where we left in the post writing-your-first-blog-app-in-django-part-18.

So upto now we have created our login and signup form using djago’s inbuilt forms.Now we will create one another form that will add a new article to articles list page.We are going to use a model form on this page,so we are not going to hard-code our HTML to create this form.We will use model form however because this is our own model because that article in our model is not built into django.So first we have to create a model form and then render it in the browser,because a user in an inbuilt model into django,whereas an article isn’t.

So to do that first of all create forms.py file in article direcotry. Now all of our model forms for this article model will go inside this file.
So create form inside forms.py file as following:

from django import forms
from . import models

class CreateArticle(forms.ModelForm):
    class Meta:
        #import model
        model = models.Article
        #fields that we want to output.
        fields = ['title', 'body', 'slug', 'thumb',]

Now go into articles/views.py file and change it according to this:

from django.shortcuts import render,redirect
from .models import Article
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from . import forms

'''
Takes in the request object.
All we are going to do in this function is we will render a template
     to the user which is going to show a list of articles.
First parameter in render is always the request object and the second parameter is
     the template we want to render and third optional is the directory
'''
def articles_list(request):
    #Retrieve all articles from database and sort them according to date
    articles = Article.objects.all().order_by('date')
    return render(request, 'articles/articles_list.html',{'articles':articles})

def articles_details(request, slug):
    #return HttpResponse(slug)
    article = Article.objects.get(slug=slug)
    return render(request,'articles/article_detail.html',{'article':article})

#if the user is not already logged in than send the user to login page first
@login_required(login_url="/accounts/login/")
def article_create(request):
    #if the request is post then retrieve the data,which user has entered in the form 
    if request.method == 'POST':
        form = forms.CreateArticle(request.POST, request.FILES)
        if form.is_valid():
            # save article to db
            return redirect('articles:list')
    else:
        form = forms.CreateArticle()
    return render(request, 'articles/article_create.html', { 'form': form })

Now go into article_create.html template and change it according to this to display all fields.

{% extends 'base_layout.html' %} {% block content %}
<div class="create-article">
    <h2>Create an Awesome New Article</h2>
    <form class="site-form" action="{% url 'articles:create' %}" method="post" enctype="multipart/form-data">
        {% csrf_token %} {{ form }}
        <input type="submit" value="Create">
    </form>
</div>
{% endblock %}

Now you can run the server and check the /articles/create/ url.You will be able to see all the fields in like body,title,slug and thumb in that page.When you create a new article it will work,but till now it not give us the article created in article list page.It just passed a validation.The form is valid therefore we are moving the user on to the article list page.

Now what we want to do is take that article and then save it to the database,but we will do it in the next post.


So in this post,we have created article create form with the help of concept of model forms in django.

Leave a Comment

Your email address will not be published. Required fields are marked *