Metronews — A news website with Django

Ramsharan Kanwar
3 min readJun 9, 2022

--

News website with Bootstrap and Django. Monolithic. [True Fullstack]

The source code can be found here.

Much of the front end was built using pre-written CSS from Bootstrap and major free HTML templating services. This is a repository with the sole purpose of showcasing my very first Django project and how I designed the entire thing as a monolithic news website. The banner post as well as the two featured posts are sorted from the backend by pushing tags on the posts. These tags are “is_banner”, “is_featured”, etc.

How to set up the project on your local machine:

  • Clone the repository on your machine
  • Run the python manage.py migrate command if you want to use SQLite, else you may set up something like PostgreSQL as such.
  • Now create a superuser using python manage.py createsuperuser command.
  • After you have done that, you have to add articles on the admin panel. Set some of them as is_featured or is_banner, etc.
  • You’re good to go.

You may use django-ckeditor to add posts. It is a very powerful text editor and much better than the method I have shown in this project(this is my very first project, hence, it doesn’t have the best convention or add-ons).

Home Page for website
from django.db import models
from django.utils import timezone
from django.conf import settings
#preventing special characters in the title like + / %
# from django.core.validators import RegexValidator
from django.utils.text import slugify
from PIL import Image


class Genre(models.Model):
category = models.CharField(max_length=30)

def __str__(self):
return str(self.category)


class Article(models.Model):
title = models.CharField(max_length=100, unique=True)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
genre = models.ForeignKey(Genre, on_delete=models.CASCADE, default=1)
excerpt = models.CharField(max_length=150, default='')
is_featured = models.BooleanField(default=False)
is_banner = models.BooleanField(default=False)
slug = models.SlugField(max_length=255, unique=True)
image = models.ImageField(blank=True, upload_to='article_images/')
is_home_article = models.BooleanField(default=False)

def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super().save(*args, **kwargs)
image = Image.open(self.image.path)
if image.height > 700 or image.width > 700:
output_size = (700, 700)
image.thumbnail(output_size)
image.save(self.image.path)

def __str__(self):
return str(self.title)


class Comment(models.Model):
commenter = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
posted_to = models.ForeignKey(Article, on_delete=models.CASCADE)
content = models.CharField(max_length=300)
time_posted = models.DateTimeField(default=timezone.now)

def __str__(self):
return str(self.commenter)

Lower Half of the Home page Shows and stationary right sidebar and a scrollable featured posts section. These are automatically sorted from the backend and will only show up on the frontend if the posts are marked as being worthy of the front page.

Users can view posts by going to the archives section. They can view previously written posts sorted chronologically. The website also has search functionality for posts. Users may login and then comment. An account is necessary to make comments.

Home page shows articles as well as archives

--

--

Ramsharan Kanwar
Ramsharan Kanwar

Written by Ramsharan Kanwar

An independent journalist and writer.

No responses yet