In this Python tutorial, I will explain to you how to create app in Django, where you will understand “what is an app and why to create?”.
Additionally, You will understand the importance of creating an app or its purpose, also I will explain to you the folder structure and files that exist within the Django app.
Finally, you will create a small blog project to understand the concept of the Django app.
How to Create App in Django
Before beginning, you need to know ‘What is an app in Django?’, An app is a modular component or considered like a module or package that is used to handle a specific functionality or features.
For example, e-commerce has different functionalities like adding items to a cart, listing products etc. These functionalities can be a different app such as an app for adding items to a cart, or an app for listing products.
Suppose you have to build a large website so you keep the functionality and features of that website in a separate app and this is how modularity is provided by creating a separate app for the specific functionalities or features in Django.
What is the idea behind the app in Django?
Creating an app in Django has two important purposes, modularity and reusability, so breaking down the project into smaller apps for specific functions, you can use these apps in different projects as needed.
So what exactly are modularity and reusability?
Modularity: An app in Django is a separate part of the Django project, suppose you need to make a blog website where users can write articles, sign in, and view the articles.
- In Django, instead of writing all these functionalities in a single place, you create three different parts, one for article writing, one for user signing in and one for viewing the articles in a separate app.
- Each app or part does its own job and can work on its own but when combined together, they make a complete website.
Reusability: As you know apps are like a module in a Django project, so you can use these apps in different projects, think that you have created an app for signing the user, you can use this app in different projects where sign-in functionality is required.
Let’s move and create an app in Django.
Creating App in Django
Before you create an app, set up the virtual environment by following the below steps:
Make sure you have installed Python, if you don’t know how to install Python refer to the tutorial on downloading and installing Python.
After that open the command prompt or terminal and type the following command to create a virtual environment named ‘app_env’.
python -m venv app_env
Activate the environment using the below code.
Then install the latest version of Django.
pip install Django
After installing a Django, create a Django project named ‘blog’ using the below command.
django-admin startproject blog
Now change the directory to blog, suppose you have to build the blog website and for that, you have just created the project ‘blog’ and this project is going to have all the apps with specific functionality of this project.
So first app is users which handles the user register and sign-in operations, create an app named ‘users’ under the project ‘blog’ using the below command.
django-admin startapp users OR python manage.py startapp users
In the above picture, you have created the app ‘users’ under the Django project ‘blog’, but there are some rules that you need to know when you create an app in Django.
And the rules are the naming conventions for the app in Django:
- As you know in Django, a project is a complete application like you created one ‘blog’ and an app is a module within that project like the app ‘users’ to handle the specific functionalities.
- So it is always good practice to name your app based on the functionality like you created app ‘users’ which may contain functionality for the user registration, sign in and sign out.
- Whenever you assign a name to your app always use the lowercase and underscores and avoid using the Python reserve keywords.
Now create one more app named ‘articles’, this app is going to contain functionalities related to articles. Use the below command.
django-admin startapp articles OR python manage.py startapp articles
Creating App in Django Folder Structure
Let’s know the structure of the apps in Django, now I am going to open the project ‘blog’ in Visual Studio code. Look at the below picture that shows the project structure.
Now here you will understand only the structure of Django apps, if you want to know the structure of a Django project, then refer to this tutorial Django Project.
Let’s expand the app ‘articles’ to understand its structure and the file inside it.
Here is the meaning and purpose of each file within the Django app ‘articles’
- migrations/: Directory to store database migration files, this file contains the information on whatever changes you want to make in the database such as adding tables, modifying existing fields or updating the database.
- admin.py: Contains admin site configurations.
- apps.py: Contains the application configuration or it contains basic information about the app that Django need to know.
- models.py: Contains database models or models for the database.
- tests.py: Contains test cases and these test cases help in making sure that everything is working as expected.
- views.py: Contains views or logic such as retrieving articles from the database and sending them to the front, basically if a user is on a different page then views decide what to show the user based on the logic defined in it.
These are the files that are automatically created when you create a new app in Django, but they can also contain additional files according to the need, and one of the most common files that are created in the Django app is the urls.py file.
The urls.py file is like a map or table of contents that contains the URLs for the specific views, whenever the user clicks on any link or URL on the website, the urls.py directs the user to a specific function (called views) which decides what type content to show the user.
But let me show you what kind of things you will write in some of the files.
So, till this point, we have successfully created two apps in the Django project. Now, let’s add some functionality to our application ‘articles’.
For this, first, we will go to the articles\views.py file and add the following code.
from django.http import HttpResponse def index(request): return HttpResponse("Welcome Everyone to PythonGuides")
In the above code, we created an index view that will simply return the given string as a response. Next, we need to map this view to a URL. For this, first, we need to create the urls.py file in our application directory. After this, we need to add the following code to the articles/urls.py file.
from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
In the above code, we have created an empty string URL pattern for our index view.
Next, we need to add our articles.urls configuration to the root urls configuration. For this, open blog/urls.py and add the following code.
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('page1/',include('sampleApp.urls')) ]
Now, simply run the development server and then open the following URL “http://127.0.0.1:8000/page1/“. And we will get the following output.
In this Python tutorial, you learned how to create an app in Django, and what is the purpose of an app in Django. Also, learned the folder structure and files of the Django app with the meaning of each file in the Django app.
You may like to read:
- How to Deploy AI Model with Django
- Python Django app upload files
- Create a Calculator app in Python Django
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.