FIRST DJANGO APPLICATION
After having created the first project with the command django-admin startproject project_name it is time to analyze the individual files created.
WE CREATE OUR FIRST APPLICATION
Let’s go to visual studio code and create our first application with the above command, positioning ourselves in the directory where the manage.py file is contained.
Let’s analyze the files that make up a Django application.
- __init__.py an empty file informing Python that the directory should be treated as a package
- admin.py used for registering models and configuring the administration interface
- apps.py contains specific configurations for our application
- models.py contains the various model classes of our application. Basically, database structure and metadata
- Migrations folder The files contained in this folder contain information regarding the relationships between models and databases
- tests.py contains functions written to test written code
- views.py contains functions that accept Http requests and return responses. Manage what the user sees
Now that we have created our first Django application we need to configure the project so that he can manage it. We open the settings.py project file and in the list of installed applications we add ours, in my case myFirstApp.
There are ready-to-use applications, such as the administration panel, authentication, static file management as these are common to every website, Django provides them for free. Now let’s go to the views.py file in the application and create our first function.
The function is very simple, it accepts a request and returns an HttpResponse with a welcome greeting. (Remember the architecture of Django). Now let’s go to the project’s urls.py file and specify the path to which the view is accessible. With this first example I want to show you the flow of a Django application.
In the figure above you can see how the view is imported and how a url is specified using the path command to map our resource to a specific url. Now let’s start the development server and go to http:// localhost:8000/homepage/. We can also include in the project’s urls.py file with the function include a new urls.py file of the application to be created and which completes access to the resource. We will come back to these topics many times, so if something has not been understood we will have ways to resume the discussion.
Leave A Comment