The first Django app (task) - 30 days of Django

The first Django app (task) - 30 days of Django

/ #Django


It's time to create our first Django app and learn what it actually is.

What is an app

First of all, we have created a Django project called Toodoo. And a Django project usually exist of multiple apps. It's not necessarily easy to explain what a Django app is, so I will give you an example.

The project we're going to build in this series will be a task manager. It's not a very complicated project, so we will probably only have one app called "Task". The Task app will consist of a few files. We will have one file where we "describe" to the database what information we want to store there, this is called a "model". There will also be a file where we create views, a folder for the templates and similar. We will go through each of them when we have created the first one.

Let's say that we also had user profiles for the project. Then we would have one more app for that. It's not impossible to have everything in one really big app, but that's really not good practice.

Creating the app

To create the app, we need to run a command. Be sure to be in the same folder as the "manage.py" file.

$ python manage.py startapp task

And that's it! You have not created your first Django app. But even though we have created it, Django doesn't actually know that it exists. So we need to append it to a list in the "settings.py" file.

INSTALLED_APPS = [
    ...
    'task',
]

As you can see, there are already a few apps in the list. These comes built into Django. It's apps for handling security, caching, messages and similar. Don't worry, we will come back to them later in this series.

So just append 'task' to the end of the list, and you're good to go :-)

What are the different files

  • task->__init__.py
  • task->admin.py
  • task->apps.py
  • task->models.py
  • task->tests.py
  • task->views.py

__init__.py
This file is empty, but it still has a function. It makes Python treat this folder as a module, so it's easy to reference the files inside it and similar.

admin.py
Here, we can register database model with the build in admin interface that Django comes with. We can also add configurations to them, so we get search, filtering and similar.

apps.py
This file is for configurations for the app.

models.py
This is where we define what information we store in the database. For example that we want a title for the task, a status, a description and similar.

tests.py
In this file, we can write tests for the app. To make sure that everything is working as it should.

views.py
A view is used to get information from the database, and render it in a template.

Summary

Just like the files in the root folder, this might be a little bit information overload. But in the next part, we will finally start coding and I promise that everything will start making more sense then.

Table of contents

Comments

No comments yet...

Add comment

Info

Please log in to comment!

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.