top of page

Blog App

In this blog, we will be learning how to make a blog app in Django using function-based views.
 

Run the below commands through cmd

django-admin startproject Blog
python manage.py startapp djangoblog

Open this project folder using any text editor for example Sublime Text, Vscode, etc.


In setting.py file of your Project i.e Blog add the name of your app.

INSTALLED_APPS = [       
 'djangoblog',     
 'django.contrib.admin',
 -----------------
 ---------------]

In urls.py file of Project, include the URL of the app. The urls.py file will look like this.

from django.contrib import admin
from django.urls import path,include
urlpatterns = [
   path('admin/', admin.site.urls),
   path('', include('djangoblog.urls')),
]

In models.py file, add the following code to it. This will make a table by name BlogApp, where we can store the title, description, author, and date of a blog.

from django.db import models
# Create your models here.
class BlogApp(models.Model):
        title=models.CharField(max_length=88)
        description=models.TextField(blank=False)
        author=models.CharField(max_length=20)
        date=models.DateField(auto_now_add=True)

In admin.py file register, the table.

from .models import BlogApp
admin.site.register(BlogApp) 

Now we have to do the migrations to save the changes we had made. Run this through cmd or by the terminal of your editor.

python manage.py makemigrations
python manage.py migrate
python manage.py runserver

U can verify the instance of your table is created or not using the admin panel.


Make forms.py file inside the app folder and insert the following code.

from .models import BlogApp
from django import forms
class BlogForm(forms.ModelForm):
           class Meta:
                model=BlogApp
                fields='__all__'


Add the following code to views.py file. With the help of these functions, we can add, update, and delete our blogs.

from django.shortcuts import
render,redirect,get_object_or_404,HttpResponseRedirect
from django.http import HttpResponse,HttpResponseRedirect
from .models import BlogApp
from .forms import BlogForm
from django.contrib import messages

# Create your views here.
def create(request):
        context={}
        form =BlogForm(request.POST or None) 
        if form.is_valid():
                form.save()
                messages.success(request, 'Blog Successfully Created')
                return redirect('create')
        context['form']=form
        return render(request, "create.html", context) 

def update(request,id):
        context={}
        obj=get_object_or_404(BlogApp,id=id)
        form=BlogForm(request.POST,instance=obj)
        if form.is_valid():
                form.save()
                messages.success(request, 'Updated Successfully')
                return HttpResponseRedirect('/'+id)
        context['form']=form
        return render(request,'update.html',context)

def index(request):
        context={}
        form=BlogApp.objects.all()
        context['form']=form
        return render(request,'index.html',context)
        
def detail(request,id):
        context={}
        form=BlogApp.objects.get(id=id)
        context['form']=form
        return render(request,'detail.html',context)

def delete(request,id):
        context={}
        obj=BlogApp.objects.get(id=id)
        if request.method=='POST':
                obj.delete()
                return HttpResponseRedirect('/')
        return render(request,'index.html',context)

Create all the HTML files which are given in views.py file. You can get it here https://github.com/prettyquail/Django-Blog/tree/manisha/djangoblog/templates


At last, we have to make urls.py file inside the app folder.

from django.urls import path
from . import views
urlpatterns=[
 path('',views.index,name='index'),
 path('create/',views.create,name='create'),
 path('update/<str:id>',views.update,name='update'),
 path('delete/<str:id>',views.delete,name='delete'),
 path('detailview/<str:id>',views.detail,name='detailview'),
 ]
 

Get the whole code here-


Happy Coding!

Follow us on Instagram @programmersdoor

Join us on Telegram @programmersdoor


Please write comments if you find any bug in the above code, or want some updates.

Follow Programmers Door for more.

26 views0 comments

Recent Posts

See All
bottom of page