visit
This ability to be self contained also means you can share your code with others and they can also use it. For instance if you didn’t want to write your own tagging library you could just run pip install taggit
and now you have a self contained tagging library that someone else wrote. Because of the way taggit was written it can be use with any other apps.
So enough prose and theory, let’s actually write some code! To start an app in Django, make sure you are in the first “tutorial” folder. I will refer to this as your ‘django project folder’ from here on (the one with manage.py
). This folder will be where all of your apps live. Now run:
$ python manage.py startapp polls$ ls
Now as you can see we now have new folder called polls
. While it may seem like startapp does some kind of magical thing to create this app, it actually doesn’t. All that happens is a folder is created with the name provided (i.e. polls), a few files are copied in, and one config file is added (apps.py). In fact you could create your own app in the terminal or in your file explorer just by creating 1 folder and 2 files. All that is required for an app folder is the folder, an __init__.py
, and an apps.py
.
So what does startapp
actually create? Let’s take a look and breakdown every file:
First there is the admin.py file. This file is where you generally put all of your configuration regarding Djangos builtin admin.
Next is apps.py, this file is used in Djangos internal app registery and is mainly used to store meta data. For the most part you won’t be modifying this much.
Then we have the __init__.py file. This file is actually Pythons convention for determining modules. When you try to run import library
or from library import x
Python will search all folders in the Python Path. If a folder has an __init__.py file, Python will also search inside of that folder, otherwise it is ignored. So if we want to be able to import code from our polls
app we need __init__.py.
Now we have the migrations folder. Migrations are how Django builds your database. We will get into this later, but you will see “migrations” added to this folder as we start working with models.
Then we have the models.py this file is where you should store all of your models related to this app. Models are how Django understands your database and how you want data stored.
Next we have tests.py. Sometimes you will see this as a tests folder instead. This file/folder is for adding unit and integration tests. This is something we will cover in much greater detail later on, but basically a test is code you write to make sure your code is running how you expected.
Finally we have views.py. This is the file where all of our business logic is stored. We will define all of our views related to this app, both Class-Based and Function, in this file. In other frameworks, you would probably call Djangos views “Controllers”.
If you’ve worked with Django or other MVC frameworks before you may notice something is missing. There isn’t anywhere to store static files or templates (views in other frameworks). By default startapp
doesn’t create these, but you can by running:
That works perfectly fine and now Django will automatically start looking for static files in polls/static
and looking for templates in polls/templates
.
Additionally a lot of third-apps use separate files similar to api.py to store configuration on a per app basis.
So now you’ve created an app, you know what everything does, but Django still does not know about your app! To make Django aware of your app you need to edit the tutorial/settings.py
file. Once you have it open search for INSTALLED_APPS
, you should see a section that looks like this:
This is a list of all of the apps that Django will use when you start up. To make Django aware of your amazing new polls app, just add 'polls',
to the end of the list: