Open In App

How to Create an App in Django ?

Prerequisite – How to Create a Basic Project using MVT in Django?

Django is famous for its unique and fully managed app structure. For every functionality, an app can be created like a completely independent module. This article will take you through how to create a basic app and add functionalities using that app.
For example, if you are creating a Blog, Separate modules should be created for Comments, Posts, Login/Logout, etc. In Django, these modules are known as apps. There is a different app for each task. 



Benefits of using Django apps –

Pre-installed apps – 

Django provides some pre-installed apps for users. To see pre-installed apps, navigate to projectName –> projectName –> settings.py 
In your settings.py file, you will find INSTALLED_APPS. Apps listed in INSTALLED_APPS are provided by Django for the developer’s comfort. 



Also, Visit :Django ORM – Inserting, Updating & Deleting Data 

Creating an App in Django :

Let us start building an app. 

Method-1

python manage.py startapp projectApp

 Method-2

django-admin startapp projectApp

To consider the app in your project you need to specify your project name in INSTALLED_APPS list as follows in settings.py:




# Application definition
 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'projectApp'
]

So, we have finally created an app but to render the app using URLs we need to include the app in our main project so that URLs redirected to that app can be rendered. Let us explore it. 
Move to projectName -> projectName -> urls.py and add below code in the header

from django.urls import include

Now in the list of URL patterns, you need to specify the app name for including your app URLs. Here is the code for it –




from django.contrib import admin
from django.urls import path, include
 
urlpatterns = [
    path('admin/', admin.site.urls),
    # Enter the app name in following
    # syntax for this to work
    path('', include("projectApp.urls")),
]

The main feature of Django Apps is independence, every app functions as an independent unit in supporting the main project. 

Now the urls.py in the project file will not access the app’s url.

To run your Django Web application properly the following actions must be taken:-

1. Create a file in the apps directory called urls.py

2. Include the following code:




from django.urls import path
#now import the views.py file into this code
from . import views
urlpatterns=[
  path('',views.index)
]

The above code will call or invoke the function which is defined in the views.py file so that it can be seen properly in the Web browser. Here it is assumed that views.py contains the following code :- 




from django.http import HttpResponse
 
 
def index(request):
  return HttpResponse("Hello Geeks")

After adding the above code, go to the settings.py file which is in the project directory, and change the value of ROOT_URLCONF from ‘project.urls’ to ‘app.urls’

From this:-

To this:

3. And then you can run the server(127.0.0.1:8000) and you will get the desired output


Article Tags :