How to create a simple blog using Django 3 - part 2

/ #Django


In this part of the tutorial series I'm going to talk about what the different files of the application we created yesterday is and what they do. We're also going to create the app which is going to contain most of the code for the blog.

File structure

db.sqlite3
This is Django's default database. It's a SQLite database and it's really easy to use because you don't need a separate server.

manage.py
This is the file you use to start the local dev server, to tell the database to add new fields and so on. It's like the "brain" of the application.

Inside the 'myblog' folder, you'll find a couple more files.

urls.py
This is where Django handles all of your websites routes/paths. It tells django what to show when you visit the frontpage or one of the other urls on your site.

settings.py
This is where you store all of your websites settings like which database to use, which parts of Django you use, where the templates are stored, if you're in debug mode or not, etc.

asgi.py / wsgi.py
These two files are helpers for your webserver. I will not talk more about these in this tutorial since they just concern you when you're ready to deploy / publish your website.

What is a Django app?

An app is a small part of your website. Like in our example, we're going to create one app for the blog. An app consists of the views, the models, forms and other things connected to the blog.

The views are functions for getting data from the database and rendering the HTML. The models are classes for defining what data the app consists of. Forms can be used for comments, contact forms, registration, etc.

It's better to have many small apps, than one big. Because smaller apps are more maintainable and this is a very important part of Django.

Create the blog app

Now that you know what a Django application is, it's time to create one. Open up your terminal and make sure that your virtual environment is activated and that you're in the correct directory (just like you learned in part one. When the terminal is ready, type this following command and hit enter:

$ python manage.py startapp blog

This will create a new folder inside the root of your website. The folder will be called "blog" and will consist of a couple of new files.

The views.py file is where you create your views. For example the detail page of a blog post is one view and the list page/category page is a different view. models.py will contain information about what data we're going to use. We need one model for the blog post, one model for the categories and one model for the comments. admin.py is a file used to tell Django what we want to be used in the admin section.

I know all of this might sound a bit confusing right now, but as soon as we start building the blog, everything will make much more sense.

Summary

In this part of the tutorial series, we talked about the structure and files of the website. We created a new app which will contain all of the views and data for our blog. In the next part, we will set up the database models and start filling the database with some dummy data.

How to create a simple blog using Django 3 series

Part 1 - Installation
Part 2 - File structure
Part 3 - Database models
Part 4 - Admin interface
Part 5 - Creating some templates
Part 6 - Views and URLs
Part 7 - Category views
Part 8 - Media files
Part 9 - Comments
Part 10 - Search


Comments

No comments yet...

Add comment

Subscribe to my newsletter