Creating our first project and application (app) in Django

- Andrés Cruz

En español
Creating our first project and application (app) in Django

We are going to explain what are the necessary steps to create a project and application in Django and what these two terms consist of in Django and with this present our first Hello World in Django, which has been the entry phase in any programming language such as Python. or framework.

Earlier we talked a bit about this popular framework for Python known as Django; therefore surely the title of this entry in which we continue to create things must shock you a bit and by things I mean a project and THEN an application.

Create a project in Django

We are going to start by creating the global element of everything that would be a project; later we will talk about projects and applications and their conception in Django; but for now, stay with the idea that a project is the most global element we have; we go to some location to create our project in Django 3 and execute:

django-admin startproject firstProject

Where firstProject is the name of the Django project you're creating.

Understanding the structure of a project in Django

Now we are going to evaluate what was created with the previous command; If we enter the folder called firstProject or the name you defined for the previous command, we will see the following files:

  • manage.py: A command line utility that allows you to interact with this Django project in different ways and you can learn more in the official documentation.
  • __init__.py: An empty file that tells Python that this directory should be considered a Python package.
  • settings.py: Settings/configuration for this Django project; defines a set of configurations that we can apply to the project globally.
  • urls.py: The URL declarations for this Django project; from here we can (for example) load more files from URLs of other applications or dependencies.
  • asgi.py: Input file to work with ASGI servers and perform the deploy.
  • wsgi.py: Input file to work with WSGI servers and perform the deploy.

Projects and applications

In the previous block we saw how we can create a new project in Django and here the keyword, we create a PROJECT and as we explained in this same entry, a Django project consists of one or many applications, which is what we are going to create in this spot; We are going to create at least one application to be able to do something interesting in our newly created project; I leave you the official documentation so you can get more information about projects and applications in Django.

Projects vs Applications - What is the difference between a project and an application?

  • An app is a web application that does something, for example, a web logging system, a public records database, a Rest API, a vending-something app, etc.
  • A project is a collection of settings and applications for a particular website. A project can contain multiple applications. An application can be in multiple projects.

Creating our first application in Django

Ok, at this point, we're finally going to create an application in Django; that as we speak it has been the mechanism to be able to develop new functionalities in Django; in other words, our web application. For that again we are going to rely on the PROJECT that we created previously, specifically with our console or terminal within the project that you created previously; which in my case is simply called firstProject; and here we are now going to execute the following command:

python manage.py startapp firstApp

With this command we have just created our first application and we are ready to work with at least a few layers of our MTV, which we will do in the next posts about Django.

Application structure

If we enter the folder that we created earlier whose name is firstProject (which is our application in Django and which is nothing more than a module in Python):

  • __init__.py: Special Python file indicating that this folder is a module.
  • admin.py: Django file in which we can register models to use them in an application that Django offers us to perform data management.
  • apps.py: Application configuration file.
  • migrations/: Folder from which we can manage migrations.
  • models.py: File from which we can create the application models and this is one of the layers of our MTV.
  • tests.py: File to test the application.
  • views.py: File to create the functions or views to make the logic and it is the business logic layer that is in charge of connecting the model layer with the template layer; this has been the view layer or -part- the controller in our MVC.

Of course, these are the initial files and folders that we can extend to add more settings like forms, urls, etc.

Register the app in the project in Django

As we talked about before, basically the project is the global element that allows one or many applications to be contained, such as the firstProject call that we created earlier; so, in order to use the app throughout our project, we need to register the app within our project; for this, we have to open the file called settings.py and locate the option called INSTALLED_APPS:

In my case it looks like the following:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'firstApp'
]

But you should only worry about registering the application you just created:

INSTALLED_APPS = [
    'firstApp'
]

And now we can use our application to be able to develop our functionalities following the MTV that we talked about earlier.

Remember that in the Django course you can get more information about everything we see here.

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.