image
Introduction to Django (Part 3)
#Python #Programming #Beginner #Django

Creating our App


This is a continuation of the previous articles I did so if you have not checked them out please do before you proceed with this one.

In this article we will now dive deep into creating our app. To create an app we run

python manage.py startapp (app_name)

You have to provide your app name so that you have something like;

python manage.py startapp blogs

This will create another folder by the name you provided. This app will be created in the root directory where you have manage.py file.

Inside your created folder there are some files which I'll try to explain what each file does, though i had talked about some in our first article(Part 1)

1. __init__.py: this is just used us so as to consider that whatever we are working on is an app, it's always blank.

2. admin.py: this is used in registering our models into our admin models 

3. models.py: this is used to create our needed tables to be saved in the database.

4. test.py: this is used for carrying out tests in our app.

5. views.py: this is responsible for handling our views, anything we want to be displayed in our templates will be done here. 

6. urls.py: this is where we will set our URL paths, though initially it's not always created, so we will create it later.

Now we need to start working in our app and we'll start by creating our tables in models.py

Creating Models

To create a table all we do is as below:

 

To create a table we have to create a class and give it a name, and make sure the name doesn't have any whitespace if you need to create a table with two words or more use an underscore to separate them. 

Now inside the class we create our field columns, data type and assign a maximum length

In my example above we have used different data types which I will explain:

1. CharField is used when you want you column to accept characters

2. TextField is used when you want maybe to have a long field that accept characters, integers etc. In TextField, you don't have to set the max_length

3. DateTimeField is used for date and time values

4. IntegerType is used for integer values in our case we are accepting 1 and 0 of which 0 is for draft and 1 for published

5. ForeignKey is used when you want to get values from another table in our case we want to fetch author from our User table.

ImageField is used for image uploads, and for you to use this you must have the pillow module installed. You can install it through

pipenv install pillow

In our imports we are impoting User table because we will be needing it in in the author column.

After all is done make sure to run 

python manage.py makemigrations

this makes sure all the records you create and saved temporarily before saving them into the database.

Now we have to migrate our fields to our database by running 

python manage.py migrate

This is supposed to run with no errors. 

Note: You need to run makemigrations before migrate, and anytime you alter your models.py file make sure to run your migrations.

That's all and upto there your models might not be visible in your admin dashboard, we'll cover that in our next article. 

So see you then and don't forget to give it an upvote if it helped you and a comment if you want me to clarify on something.