Menu Close

How to Create an App in the Django Application

how to create app in Django application

In this Django tutorial, we are going to learn all about What is Django App and how to create an app in the Django application. In the previous Django tutorial, we have seen all about how to create the first Django project.
Before going to deep into the article we will see the difference between the Django project and Django app so that you can get more clarity about the Django app and Django project.

What is Django App?

Django Apps are separate modules or components in the Django application, for example suppose you want to build an e-commerce application on Django. Then it might be, that your application can have user, profile, cart, products, address, and payment modules to store the corresponding details so all these separate modules or components in the Django application represent the Django apps. A big Django application is a combination of more than one Django app.

Built-in Django Apps

When you create the Django project first time it automatically generates some built-in apps. To see the built-in apps you have to traverse the projectName > projectName > settings.py file inside the INSTALLED_APPS section.

create an app in the Django application

Difference between the Django app and Django project

Django Project

  • In Django, Project is basically Python packages that represent the whole web applications.
  • Django project contains all the configurations and settings of entire web applications.
  • A Django project is more than a Django app that represents the functionality of the entire web application.

When you will create Django project first time, the structure will look like this.


myproject
│───manage.py
│
├───myproject
│       asgi.py
│       settings.py
│       urls.py
│       wsgi.py
│       __init__.py

Django App

  • Basically, Django apps are a sub-module of Django applications.
  • Django apps are loosely coupled which means all the apps in the Django project are independent.
  • Django apps are reusable.
  • A Django app can represent a separate module or component of your Django application.
  • Multiple developers can work on separate components.
  • Debugging the Django application is easier because Django has a strong debugging tool.

The structure of the Django app looks like this.


myapp
│   admin.py
│   apps.py
│   models.py
│   tests.py
│   urls.py
│   views.py
│   __init__.py
│   
├───migrations
│   │   __init__.py

Now it’s time to create an app in the Django application.

How to create an app in Django Application

In order to create the Django app, you have to follow all these steps.

If you don’t know how to create a Django project then click here.

  • Open your Django project in your favorite editor, Here I am using PyCharm.
  • Click on Terminal from the bottom section.
create an app in the Django application
  • Use the below command in order to create the Django app.

cd myproject
django-admin startapp myapp
create an app in the Django application

myproject is the name of the Django project, and myapp is the name of the Django app.

Note:- If you are already inside the Django project directory then you can ignore the first command.

Once your Django app is successfully built, your Django project structure will look like this.

create an app in the Django application
  • After creating the name of the Django app you have to register it in the settings.py file inside the INSTALLED_APPS section.
create an app in the Django application
  • After completing the above steps you have to map your Django app to your Django project URLs.

from django.contrib import admin
from django.urls import path
from django.urls import include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("myapp.urls"))
]
  • Create an urls.py file inside the Django app directory along with this code.

from django.urls import path
from .views import home


urlpatterns = [
    path('home', home, name="home"),
]
  • As you can see in the above code, we have imported the function home from the views.py file, now it’s time to define the home function inside the views.py file in the Django app directory.

from django.http import HttpResponse


def home(request):
    return HttpResponse("<h1> First Django App is created successfully...</h1>")
  • Issue the below command to run the Django development server.
python manage.py runserver
  • Visit http://127.0.0.1:8000/home to see the result. Once you will visit this URL you will see the following interface.

Conclusion

So, finally, we have seen all about the Django app and also seen how to create an app in the Django application with the help of proper guidance so that you don’t have any confusion regarding how to create a Django app.

Django project is a simple Python package that contains all the configurations and settings of Django applications. Django app is a separate module or component in the entire Django application which runs independently and it is reusable also.

I hope this article will be helpful for you, If you like this article please share and keep visiting for further amazing Django tutorials.

Create First Django Project
Django Models Tutorial - Beginner to Advanced

Related Posts