Django Blog CMS in Python
About the project: This is a guide to creating a basic Blog Content Management System (CMS) using the Django framework. The project will allow you to create, view, and manage blog posts.
It is to get started on a Django Blog CMS project. However, a complete Django application is not contained in a single file. It requires a specific project structure with multiple files for models, views, templates, and URL routing.
Project Level: AdvanceProject Overview
- Models: Define the structure for your blog posts (e.g., title, author, content).
- Admin: Register your models with the Django admin interface to easily create and edit blog posts.
- Views: Handle the logic for displaying blog posts and the main page.
- URLs: Map URLs to the correct views.
- Templates: Use HTML files to render the content.
Prerequisites
Before you begin, make sure you have Python installed. Then, install Django using pip:
pip install django
Getting Started
1. Create the Project
Open your terminal or command prompt and run these commands to set up the project and a new application:
django-admin startproject blog_project
cd blog_project
python manage.py startapp blog
2. Update settings.py
Open the blog_project/settings.py file and add 'blog' to the INSTALLED_APPS list.
# blog_project/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog', # Add your new app here
]
# ... rest of the file
3. Create the Blog Post Model
Now, create the database model for your blog posts in blog/models.py. This model will define the fields for each post.
# blog/models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
pub_date = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
4. Register the Model in the Admin Interface
To manage your posts easily, register the Post model with the Django admin site. Open blog/admin.py and add the following code:
# blog/admin.py
from django.contrib import admin
from .models import Post
admin.site.register(Post)
5. Create Views
Views contain the logic for your application. In blog/views.py, you will create a view to display all posts on a single page.
# blog/views.py
from django.shortcuts import render
from .models import Post
def post_list(request):
posts = Post.objects.all().order_by('-pub_date')
return render(request, 'blog/post_list.html', {'posts': posts})
6. Set Up URL Routing
URLs connect your views to a specific address. First, create a new file blog/urls.py and add this code:
# blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.post_list, name='post_list'),
]
Next, include this new URL file in the main project's urls.py.
# blog_project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')), # Include the blog app URLs here
]
7. Create HTML Templates
Finally, create a directory for your templates and the HTML file to display the blog posts.
Create the following directory structure: blog_project/blog/templates/blog/
Inside this final directory, create a new file named post_list.html:
<!DOCTYPE html>
<html>
<head>
<title>My Blog</title>
</head>
<body>
<h1>Latest Blog Posts</h1>
{% for post in posts %}
<div style="border: 1px solid #ccc; padding: 10px; margin-bottom: 20px;">
<h2>{{ post.title }}</h2>
<p>By {{ post.author.username }} on {{ post.pub_date }}</p>
<div>{{ post.content|linebreaks }}</div>
</div>
{% empty %}
<p>No posts are available.</p>
{% endfor %}
</body>
</html>
Running the Project
Now you are ready to run the project. In your terminal, from the blog_project directory, execute these commands:
- Migrate the database: This creates the necessary tables for your models.
python manage.py makemigrations python manage.py migrate
- Create an admin user: You'll need an account to create posts. python manage.py createsuperuser
- Run the server:
python manage.py runserver
Now, navigate to http://127.0.0.1:8000/admin/ to log in and start creating your first blog posts!
← Back to Projects