Django App

1. What is django app

In Django, an app (short for "application") refers to a self-contained module or component that provides a specific functionality within a Django project. An app is a directory containing Python modules, templates, static files, and other resources that work together to implement a particular feature or set of features.

A Django app typically consists of the following components:

  1. Models: Models define the structure of data and represent database tables. They are defined as Python classes that inherit from Django's models.Model class. Models define fields, relationships, and behaviors of the data.
  2. Views: Views handle the logic of processing HTTP requests and generating responses. They are Python functions or classes that receive requests, interact with models or perform other operations, and return responses, usually in the form of HTML pages.
  3. Templates: Templates are HTML files that define the presentation and structure of the generated web pages. They can include placeholders and template tags to dynamically display data passed from views.
  4. URLs: URLs (Uniform Resource Locators) define the URL patterns and map them to specific views within the app. They determine which view is called for a particular URL.
  5. Static files: Static files include CSS stylesheets, JavaScript files, images, and other assets used by the app. They are served directly by the web server and are not processed by Django.
  6. Forms: Forms are used to handle user input, validate data, and interact with models. Django provides form handling functionality to simplify form creation and processing.
  7. Admin: Django's admin interface allows easy management and manipulation of the app's data through a web-based administrative interface. It is automatically generated based on the app's models and can be customized to fit specific requirements.

Apps in Django are designed to be modular, reusable, and pluggable. They promote code organization, separation of concerns, and maintainability. Multiple apps can be created within a Django project, each responsible for a specific feature or aspect of the overall application. Django provides tools and conventions to manage and integrate apps into a project seamlessly.

2. How to create a django app

In this paragraph, we will see how to create a django application named myapp within a django project named mysite
To create a Django app called "myapp" within a project called "mysite", you can follow these steps:

- Open your terminal or command prompt and navigate to the directory where you want to create your Django project.
- Create a new Django project called "mysite" by running the following command:

- Change into the project directory by executing:

- Create a new Django app within the project by running the following command:

After running the command, a new directory named "myapp" will be created. Inside this directory, you'll find various files and folders, including models.py, views.py, urls.py, and a templates folder.
- Define your app's models by opening the models.py file inside the "myapp" directory and creating Python classes that inherit from models.Model. Define fields, relationships, and any additional methods required for your app's data. (We will see all this with more details in the next tutorials)
- Implement the logic for your app's views by opening the views.py file inside the "myapp" directory. Define Python functions or classes that handle HTTP requests and generate responses. These views can interact with models and perform other operations as needed.

- Define URL patterns for your app by creating a new urls.py file inside the "myapp" directory. You can define URL patterns specific to your app and map them to the corresponding views.

To use your newly created app, you need to add it to the project's settings. Open the settings.py file inside the "mysite" directory, locate the INSTALLED_APPS list, and add the name of your app, which is "myapp", to the list:

3. Hierarchical structure of django app

To get an idea about the structure of a django app, here's a hierarchical structure of a Django project called "mysite" with a Django app called "myapp":

Explanation of the directory structure:

  1. The outer "mysite" directory: is the root directory of the Django project.
  2. The inner "mysite" directory: inside the "mysite" directory. This inner directory contains the project-level settings, URLs, and other project-related files.
  3. The "myapp" directory: is created within the project and represents the Django app named "myapp".
  4. The "migrations" directory: inside the "myapp" directory, which contains database migration files generated by Django for managing database schema changes.
  5. The "templates" directory: within the "myapp" directory is where you can store HTML templates specific to the "myapp" app.
  6. The "init.py" files: are required to treat the directories as Python packages.
  7. The "admin.py" file: is used to customize the Django admin interface for the "myapp" app.
  8. The "apps.py" file: holds the configuration for the "myapp" app.
  9. The "models.py" file: is where you define the models (database tables) for the "myapp" app.
  10. The "tests.py" file: is for writing tests specific to the "myapp" app.
  11. The "views.py" file: contains the views (functions or classes) responsible for handling HTTP requests and generating responses for the "myapp" app.
  12. The "manage.py" file: which we have already talked about, is the Django project management script.

This hierarchical structure illustrates the typical layout of a Django project with an app. It allows for modular development and organization of code, separating the project-level components from the app-specific components.

4. Settings app the url of myapp

4.1 Define the 'myapp' view

In order to be able to display our django application 'myapp', we must first define the view of the application by editing the file: myapp/views.py. We will add to the latter a function that will generate the view of the application 'myapp':

4.2 Set up the url for "myapp"

To set up the URL for our "myapp" app within our Django project, we need to follow these steps:

  • Within the "myapp" directory, create a new file called urls.py if it doesn't already exist.
  • Open the newly created urls.py file within the "myapp" directory.
  • Define the URL patterns specific to  "myapp" app within the urls.py file. Here's an example to get you started:

 

4.3 Declaration of the  "myapp/urls.py"   file at the   "mysite/urls.py"   level:

we must at first, include function from the django.urls module by adding the following import statement at the top of the mysite/urls.py file:

Add a new URL pattern for "myapp" app by including the URL patterns:

This line defines a URL pattern that maps any URL starting with "myapp/" to be handled by the URL patterns defined in our "myapp" app's urls.py file.

Finally, here is the code of mysite/urls

 




5. Launch myapp in the browser

To launch "myapp" in the browser, we need to start the Django development server. Here's how you can do it:

  1. Open your terminal or command prompt.
  2. Navigate to the root directory of your Django project (the directory containing the manage.py file).
  3. Start the development server by running the following command:

  • After executing the command, you should see output similar to the following:
  • Starting development server at http://127.0.0.1:8000/
  • Quit the server with CTRL-BREAK.

Now, when you run your Django development server and navigate to the URL "myapp/", it will be handled by the URL patterns defined in your "myapp" app's urls.py file, directing the request to the specified view function.
Open your web browser and enter the following URL: http://127.0.0.1:8000/myapp/

first django app

This URL represents the local development server running on your machine and accessing the "myapp" app.

We should now be able to see our "myapp" running in the browser. If we have defined a view function like myapp_home() in our "myapp" app's views.py file and configured the URL pattern correctly, the response from that view function will be displayed in the browser.

Leave a Reply