Django News App Django.How

Author avatar wrote on 26/05/2022

  • Create App
  • Configuration Admin Area
  • Allowing Managing Careers Using Admin Panel
  • Build Front Facing Page
  • Create View
  • Creating URLs
  • Create HTML Template for this App

Create App

  1. Type python startapp news news is an example of app name

  2. Go to the file news/ and declare your class: import all classes we need

from django.db import models
from datetime import datetime
from django.contrib.auth.models import User

3.Declare class for Categories

class Categories(models.Model):
    title = models.CharField(max_length=200)
    def __str__(self):
        return self.title

4.Declare class for News

5.Declare class for Comments

class Comments(models.Model):
    author_name = models.CharField(max_length=200, blank = True)
    author_email = models.CharField(max_length=200, blank = True)
    author_site = models.CharField(max_length=200, blank = True)
    comment_date = models.DateTimeField(, blank = True)
    comment_text = models.TextField()
    comment_post = models.ForeignKey(News, on_delete=models.CASCADE)
    def __str__(self):
        return self.author_name

NOTICE: for correct work Pillow plugin have to be installed.

Type pip install pillow into your terminal

1.Add some piece of code to our main settings file of the project:

#Media folder settings
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'

2.Add some piece of code to our main file:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

Add News app to the installed app



Make Migrations

After this create the migration, make sure in the terminal you are in the right folder and pipenv shell is running then type python makemigrations news this will create the necessary python files, and run migration by typing python migrate command

Configuration Admin Area

Allowing Managing News, Categories and Comments Using Admin Panel

Open news/ file and import the new models and register them under admin:

Notice: must be installedSummernote app. Detaile info how to install it you can find here

Build Front Facing Page

Create View

We will have three pages: with a list of all news or filtered by category, and detailed post page, so inside news/ we need to define three functions.

1.Bring the models

from django.shortcuts import render, get_object_or_404, redirect
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator

from .models import News, Comments

2.Declare first function for main page with list of all news:

3.Declare function for page with news filtered by category:

4.Declare function for single post page:

Creating URLs

1.Create file in app folder

2.Add URL rules to this file<

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='news'),
    path('',, name='post'),
    path('category/',views.category, name='category')

3.In the main app folder, oper file and include the file you created

urlpatterns = [
    path('news/', include('news.urls')),

Create HTML Template for this App

1.Create news folder inside templates

Create index.html and news.html file inside templates/news folder

Copy html code to this file from here: