In our last blog post, we covered the basic concepts of Django. Now, let's create a simple blog application to solidify our understanding.
Requirements:
- Basic Python knowledge
- Fundamental understanding of Django
- Basic proficiency in using the terminal
To create a simple blog, we will follow these steps:
Step 1: Create a project folder and Install the Virtual Environment
- First, create a folder called
Simple-blog
, then inside the folder, install the virtual environment and activate it.
Step 2: Install Django
- Once your virtual environment is activated, install Django using pip, Python's package installer.
pip install django
Step 3: Set Up the Project and App
Create a Django project:
- Create your django project and cd into it.
mkdir Simple-blog
django-admin startproject myblog
cd myblog
- After running the
django-admin startproject
you should have the following structure:
myblog/
manage.py
myblog/
__init__.py
settings.py
urls.py
wsgi.py
asgi.py
Create a Django App:
Inside your django project folder, run the following command to create an app:
# Create a new app named 'blog'
python manage.py startapp blog
Configure the App
To use the new app in your project, add it to the INSTALLED_APPS list in the settings.py file.
# myblog/settings.py
INSTALLED_APPS = [
...
'blog',
]
After following all instructions, you should have the complete structure as follows:
myblog/
manage.py
myblog/
__init__.py
settings.py
urls.py
wsgi.py
asgi.py
blog/
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
migrations/
Step 4: Define the model
- On the model file
blog/models.py
add the following to define thepost
model:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
published_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
Run Migrations:
Migrations in Django are like version control for your database schema. They're a way to apply and un-apply changes to your database schema, which include creating new tables, changing fields, or deleting tables. Django creates these migration files for you, and you can apply them using the
makemigrations
and migrate management commands. This system allows Django to automatically provide schema alteration operations when new changes are made to models.The
makemigrations
command in Django creates new migration files based on the changes you've made to your models. It doesn't affect your database yet.On the other hand, the
migrate
command applies these migrations to your database. It reads the migration files and applies the corresponding changes to your database schema.
To run migrations:
python manage.py makemigrations
python manage.py migrate
N/b Remember, makemigrations
should be run when you've made changes to your models and want to create new migrations, and migrate
should be run to apply these migrations to your database.
Step 5: Register the model with Admin
- On the admin file
blog/admin.py
add the following to register your models with admin:
from django.contrib import admin
from .models import Post
admin.site.register(Post)
Step 6: Create a superuser
- Run the following command on your terminal and follow the steps to create a superuser.
python manage.py createsuperuser
- Run the server using
python manage.py runserver
, navigate tohttp://127.0.0.1:8000/admin/
, and use your already created superuser credentials to access the admin site.
Step 7: Define the View
- On the views file
blog/views.py
add the following to create your app views:
from django.shortcuts import render
from .models import Post
def home(request):
posts = Post.objects.all()
return render(request, 'blog/home.html', {'posts': posts})
Step 8: Configure the URL
- Create a blog/urls.py file and add the following to map the URL to your view:
from django.urls import path
from blog import views
urlpatterns = [
path('home/', views.home, name='home'),
]
- Then, register your app URLs
blog/urls.py
to your django project inmyblog/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')), # Add this line
]
Step 9: Create the Template
- Create
blog/templates/blog/home.html
and the following to structure and present our blog as a webpage:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blog Home</title>
</head>
<body>
<h1>Blog Posts</h1>
{% for post in posts %}
<h2>{{ post.title }}</h2>
<p>{{ post.content }}</p>
<p>Published on: {{ post.published_date }}</p>
{% endfor %}
</body>
</html>
Step 10: Run the Development Server
- Finally, let's run our development server using the following command:
python manage.py runserver
- Navigate to
http://127.0.0.1:8000/home/
to see your simple blog application in action.
- Use the admin site
http://127.0.0.1:8000/admin/
to create a blog post and see it live on your simple blog.
Conclusion and Next Steps
By understanding Django's project structure, MVT architecture, and the Django admin interface, you now have the foundational knowledge to build web applications. In the next part of this series, we'll dive deeper into Django's ORM, explore how to create more complex views and templates and implement additional functionality to enhance our blog application. Stay tuned!
Top comments (0)