Setting up the Django Project and App First, ensure Django is installed and set up a new Django project and app:
Create a New Project (if not already created):
django-admin startproject mysite
cd mysite
Create a New App (e.g., 'blog'):
python manage.py startapp blog
Include the App in Your Project:
Open mysite/settings.py. Add 'blog' to the INSTALLED_APPS list.
- Defining the Model In the blog app, modify the models.py file to define the BlogPost model:
from django.db import models
class BlogPost(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
published_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
```
Migrate the Model:
Run python manage.py makemigrations blog to create migrations for the changes.
Run python manage.py migrate to apply migrations to the database.
3. Creating Views and URL Routing
Define a View in views.py of the blog app:
from django.shortcuts import render from .models import BlogPost
def blog_list(request): posts = BlogPost.objects.all().order_by('published_date') return render(request, 'blog/blog_list.html', {'posts': posts})
Set up URL Routing:
Create a urls.py in the blog app directory.
Define the URL pattern for the view:
from django.urls import path from . import views
urlpatterns = [ path('', views.blog_list, name='blog_list'), ]
Include blog URLs in the main URLconf (mysite/urls.py):
from django.urls import include, path
urlpatterns = [ path('blog/', include('blog.urls')), # other paths... ]
4. Designing the Template
Create a Template File:
Create a directory named templates within the blog app.
Inside templates, create another directory named blog.
Create a file blog_list.html inside blog/templates/blog/.
Design the Template (blog_list.html):
-
{% for post in posts %}
-
{{ post.content }}
Published on: {{ post.published_date }}
{% endfor %}
- Registering with the Admin Interface Register the Model in admin.py of the blog app:
from django.contrib import admin
from .models import BlogPost
admin.site.register(BlogPost)
Create a Superuser and Test the Admin Interface:
Run python manage.py createsuperuser and follow the prompts.
Run the server python manage.py runserver.
Go to http://127.0.0.1:8000/admin
, log in, and test adding, editing, and deleting blog posts.
Running the Application
Start the Django development server:
python manage.py runserver
Access the blog list view by navigating to http://127.0.0.1:8000/blog/
.