![]() |
VOOZH | about |
We will build a straightforward Django app where users can view posts and add comments, all using just two simple templates. We will learn how to set up models, handle form submissions and create clean views to power an interactive comment system. Itβs perfect for getting a solid foundation with Djangoβs core features while keeping things neat and minimal.
Prerequisites:
Open your terminal or command prompt and run:
django-admin startproject comment_project
cd comment_project
python manage.py startapp comments
Explanation:
Open comment_project/settings.py and add the comments app to your installed apps:
INSTALLED_APPS = [
# Default Django apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',# Our custom app
'comments',
]
Edit comments/models.py to add two models: Post and Comment.
Explanation:
Run migrations to create the corresponding tables in the database:
python manage.py makemigrations
python manage.py migrate
Register your models in comments/admin.py to manage posts and comments via the admin panel:
Create comments/forms.py:
Explanation: The form renders a textarea without a label and includes a placeholder for user guidance.
Edit comments/views.py:
Explanation:
Create comments/urls.py:
Include this in your main project URL config comment_project/urls.py:
Create the directory structure:
comments/
βββ templates/
βββ comments/
βββ home.html
βββ post_detail.html
home.html:
post_detail.html:
Create a superuser to add initial posts via the admin panel:
python manage.py createsuperuser
Start the development server:
python manage.py runserver
Visit http://127.0.0.1:8000/admin/ and log in to create posts.
Visit http://127.0.0.1:8000/ to see all posts.
Click a post title to view and add comments.