[go: up one dir, main page]

Open In App

How to Create an App in Django ?

Last Updated : 22 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 –

  • Django apps are reusable i.e. a Django app can be used with multiple projects.
  • We have loosely coupled i.e. almost independent components
  • Multiple developers can work on different components
  • Debugging and code organization is easy. Django has an excellent debugger tool.
  • It has in-built features like admin pages etc, which reduces the effort of building the same from scratch

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

  • To create a basic app in your Django project you need to go to the directory containing manage.py and from there enter the command :
python manage.py startapp projectApp

 Method-2

  • To create a basic app in your Django project you need to go to the directory containing manage.py and from there enter the command :
django-admin startapp projectApp
  • Now you can see your directory structure as under :

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

Python3




# 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 –

Python3




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")),
]


  • Now You can use the default MVT model to create URLs, models, views, etc. in your app and they will be automatically included in your main project.

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:

Python3




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 :- 

Python3




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



Previous Article
Next Article

Similar Reads

Create Word Counter app using Django
In this article, we are going to make a simple tool that counts a number of words in text using Django. Before diving into this topic you need to have some basic knowledge of Django. Refer to the below article to know about basics of Django. Django BasicsHow to Create a Basic Project using MVT in Django ? Now moving forward to make our wordcounter,
3 min read
Create Newsletter app using Mailchimp and Django
In this article, we'll use MailChimp and Django to make a simple mailing app. When people subscribe to our app, we'll learn how to create an audience list and save the users in MailChimp using Python. Have you considered how they could send emails to subscribers with only one click? MailChimp is the greatest email marketing software. It also allows
5 min read
Create a Counter App Using React, Tailwind and Django Framework
This article will guide you in creating a Counter using React and Tailwind with the Django Framework. We’ll explore the integration of Django, React, and Tailwind, and go through the step-by-step process of implementing the Counter in Python using the Django Framework. What is Counter App?The Counter app is a straightforward tool designed for easy
6 min read
Adding Tags Using Django-Taggit in Django Project
Django-Taggit is a Django application which is used to add tags to blogs, articles etc. It makes very easy for us to make adding the tags functionality to our django project.Setting up Django Project Installing django-taggit pip install django-taggitADD it to Main Project's settings.py file C/C++ Code INSTALLED_APPS = [ 'django.contrib.admin', 'dja
2 min read
How to customize Django forms using Django Widget Tweaks ?
Django forms are a great feature to create usable forms with just few lines of code. But Django doesn't easily let us edit the form for good designs. Here, we will see one of the ways to customize Django forms, So they will look according to our wish on our HTML page. Basically we will check the methods to include our own custom css, classes, or id
3 min read
Integrating Django with Reactjs using Django REST Framework
In this article, we will learn the process of communicating between the Django Backend and React js frontend using the Django REST Framework. For the sake of a better understanding of the concept, we will be building a Simple Task Manager and go through the primary concepts for this type of integration between React js and Django. Reactjs in a nuts
18 min read
Styling Django Forms with django-crispy-forms
Django by default doesn't provide any Django form styling method due to which it takes a lot of effort and precious time to beautifully style a form. django-crispy-forms solves this problem for us. It will let you control the rendering behavior of your Django forms in a very elegant and DRY way. Modules required:django : django installdjango-crispy
1 min read
Python | Django News App
Django is a high-level framework which is written in Python which allows us to create server-side web applications. In this article, we will see how to create a News application using Django. We will be using News Api and fetch all the headline news from the api. Read more about the api here news api.Do the Following steps in command prompt or term
2 min read
Weather app using Django | Python
In this tutorial, we will learn how to create a Weather app that uses Django as backend. Django provides a Python Web framework based web framework that allows rapid development and clean, pragmatic design. Basic Setup - Change directory to weather – cd weather Start the server - python manage.py runserver To check whether the server is running or
2 min read
Django App Model - Python manage.py makemigrations command
According to documentation, Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They’re designed to be mostly automatic, but you’ll need to know when to make migrations, when to run them, and the common problems you might run into. makemigrations are run thro
2 min read
Practice Tags :