In this task, you will create a simple Blog project from start to end.
Let's go through all the steps:
-
Create a virtual environment, activate it, and install django via pip.
-
Start project
blog_systeminside the current directory (add . at the end of the command). -
Inside
py-blogstart applicationblog. -
Inside
blog/models.pycreate models according to this diagram: -
Edit
admin.py:- Register all your models in the admin.
- Unregister
Group(you can make it withadmin.site.unregister(Group)by importingGroupfromdjango.contrib.auth.models). - Do the filtering and searching in any way you think is logical.
-
Make migrations and migrate.
-
Use
python manage.py testto run tests. -
Use the following command to load prepared data from the fixture to test and debug your code:
python manage.py loaddata blog_system_db_data.jsonFeel free to add more data using the admin panel if needed.
-
Inside
blog_system.urlsadd a path to theblog.urls. Don't forget to specify the namespace. -
Inside
blog.urlscreate a path for the home page. Give this path the nameindex. -
Inside
blog.viewscreate a view for theindexurl. This view returns a list of all posts, in descending order bycreated_time. -
Before creating a template, you have to create styles. Create a directory
staticnext to the directoryblog. Inside this directory create a file with the following pathcss/styles.css. Don't forget to do all the necessary steps so that Django can serve these static files. -
Create a directory
templatesnext to the directoryblog. There you will store templates for pages. Edit settings so that engine knows where to look for template source files. -
Create a template for the main page. On this page, make it so that a list of all posts is displayed. The title and content were displayed, the author was visible when this post was created and the number of comments on the post. Make the post's title a link to the detailed page, which you would implement further.
-
Add pagination for the main page. Set 5 posts on one page by default.
-
Create a
PostDetailViewview that returns detailed information about the post by theidfield. -
Add a template for this page and url with a path
posts/pk/and namepost-detail. -
On the post detail page, display a list of post comments below this post.
-
Under the list of comments, add a form that allows you to create a new comment to the post.
Please note: only authorized users can post comments. If an anonymous user tries to create a new comment, the form must be invalid.
- Use crispy forms in your forms to make the website more beautiful.
- Use
python manage.py testto run tests. - Don't forget to add the
.gitignorefile before pushing. Don't push a lot of extra files(venv,pycache,.idea, etc.).
- Attach screenshots to the comment, NOT in the commit.
- It's important to attach images not links to them.
Notice: that example is not a reference! You can make this page in another way.
Follow these steps if you need to use crispy_forms v2.0 with Python 3.11:
- Add
CRISPY_TEMPLATE_PACKtosettings.py.
CRISPY_TEMPLATE_PACK="bootstrap4"- Add these apps to
INSTALLED_APPSand install them corresponding to theCRISPY_TEMPLATE_PACKbootstrap version.
INSTALLED APPS = [
...,
"crispy_bootstrap4",
"crispy_forms",
]


