Skip to content

Academy-Omen/django-blogx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build A Blog With Django

-> Download starter files for this project

-> Create Virtual environment

# Windows
py -3 -m venv env
# Linux and Mac
python -m venv env

-> Activate environment

# Windows
.\env\Scripts\activate
# Linux and Mac
source env/bin/activate

-> Install Requirements

pip install -r requirements.txt

-> Create Django project in the present directory

# the '.' tells python to create the project in the present folder
django-admin startproject core .

-> Create Blog app

python manage.py startapp blog

-> Register blog app in project settings file

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # register blog here
    'blog',
]

-> Apply migrations

python manage.py migrate

-> Create basic views

from django.shortcuts import render


def home(request):
    return render(request, 'index.html')


def article(request):
    return render(request, 'article.html')

-> Create the blog app urls file

from django.urls import path
from . import views

app_name = 'blog'

urlpatterns = [
    path('', views.home, name='homepage'),
    path('post/', views.article, name='article'),
]

-> Register the blog urls file in the project urls file

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    # import include and add blog urls.py
    path('', include('blog.urls', namespace='blog')),
]

-> Create blog app template directory in blog app directory

<!-- Example Home page -->
<h1>Home Page</h1>

-> Configure static files in settings file

import os

STATIC_URL = '/static/'
MEDIA_URL = '/media/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

-> Place the css files in static/css, images in static/images and the html in blog/templates

-> Load the static files in the html files

<!-- place this at top -->
{% load static %}

<!-- example -->
<link rel="stylesheet" href="{% static 'css/style.css' %}"">

-> Create Models and register to admin interface

python manage.py makemigrations
python manage.py migrate
#  create superuser
python manage.py createsuperuser
python manage.py runserver
# blog admin.py file
from django.contrib import admin
from . import models


admin.site.register(models.Tag)
admin.site.register(models.Profile)



@admin.register(models.Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = ('headline', 'status', 'slug', 'author')
    prepopulated_fields = {'slug': ('headline',), }

-> Tell django where to get static files in development

# core.urls.py
from django.conf.urls.static import static
from django.conf import settings

# .
# .

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

-> Add ckeditor to installed app and add the settings

# settings.py
INSTALLED_APPS = [

# .
# .

    'ckeditor',
    'ckeditor_uploader',
]

# CKEditor settigs

CKEDITOR_UPLOAD_PATH = 'uploads/'

CKEDITOR_CONFIGS = {
    'default': {
        'toolbar': 'full',
        'height': 300,
        'width': '100%',
    },
}

-> Add ckeditor urls

# core.urls.py
# .
# .

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls', namespace='blog')),
    path('ckeditor/', include('ckeditor_uploader.urls')),
]

-> Collect statics all static so as to copy ckeditor required media files

python manage.py collectstatic

-> Add content to your database

-> Update home views and add load data on template

def home(request):

    # feature articles on the home page
    featured = Article.articlemanager.filter(featured=True)[0:3]

    context = {
        'articles': featured
    }

    return render(request, 'index.html', context)

-> Update articles views and add load data on template

# Django Q objects use to create complex queries
from django.db.models import Q

def articles(request):

    # get query from request
    query = request.GET.get('query')
    # print(query)
    # Set query to '' if None
    if query == None:
        query = ''

    # articles = Article.articlemanager.all()
    # search for query in headline, sub headline, body
    articles = Article.articlemanager.filter(
        Q(headline__icontains=query) |
        Q(sub_headline__icontains=query) |
        Q(body__icontains=query)
    )

    tags = Tag.objects.all()

    context = {
        'articles': articles,
        'tags': tags,
    }

    return render(request, 'articles.html', context)

-> Add get_absolute_url to Article model which will be used to get a single article

# models.py file

    def get_absolute_url(self):
        return reverse('blog:article', args=[self.slug])

    class Meta:
        ordering = ('-publish',)

-> Update the blog urls file

# .
# .

urlpatterns = [
    path('', views.home, name='home'),
    path('articles/', views.articles, name='articles'),

    # update the article url
    path('<slug:article>/', views.article, name='article'),
]

# .
# .

-> Update articles views and add load data on template

def article(request, article):

    article = get_object_or_404(Article, slug=article, status='published')

    context = {
        'article': article
    }

    return render(request, 'article.html', context)
    

About

This is a Django 3 Blog website with a Rich Text Editor

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published