Create your First Django App

The word “App/Apps” can be confusing to Django Beginners. When you create a web app in Django, sometimes you might want to have a separate component dedicated and treated as an application. For example, when you’re developing a Portfolio Application, if you want to add a Projects section but at the same time you don’t want to include the application in the Project Component, then Django Applications can be a lifesaver.

In the Web Development Industry, it is considered good practice to separate components as Apps within the same project.

Why Use Django apps ?

  • Django Applications can be used more than once i.e. if you’re writing a code for Sign Up Page and if you feel that you would be using the Sign Up Page in the future, Instead of writing code in the current Django Project you can simply create a Sign Up App and reuse it later .
  • Components are loosely coupled in Django applications so they are independent components of the project.
  • Developers can work on different components , i.e.different people can work on different functionalities, without disturbing the project.
  • Debugging and Code Organization becomes easy which increases developer productivity.
  • Applications also come with inbuilt features such as separate admin Page .

Creating Django Application:

In the above Project Structure, there are two directories: PythonGeeks and DjangoApp.

PythonGeeks Folder is created as a default project subdirectory folder under the Project Name “PythonGeeks” whereas “DjangoApp” is a separate directory that is created after files and folders under “PythonGeeks” have been created.

1. Activate Virtual Environment:

Activate your virtual environment before trying to create Django App using the following command while you’re in the Project Directory :

venv/scripts/activate

2. Initialize Django Application:

Let us try to create an application in Django using the following command :

python manage.py startapp articles

Alternatively you can use the following command to create a Django application :

django-admin startapp projectApp

We have a new folder created under the name “articles” and our project structure would look something like this :

Now we have a Django application by name – “articles” under our root directory. A dedicated Django App helps developers to reuse the components or turn off/on the components.

The application would create the following default files :

  • migrations(folder)
  • __init__.py
  • admin.py
  • apps.py
  • models.py
  • tests.py
  • views.py

Delete the test – “Django App” folder to minimize the clutter and focus on our app.

3. Registering the Application in “urls.py” and INSTALLED_APPS:

Following this step is mandatory if your application needs to work in harmony.
When a user visits the “articles” URL, Django looks up for “articles” under urls.py and INSTALLED_APPS section to display the webpage.

Navigate to Settings.py of our main project folder and add “articles” to INSTALLED_APPS Section for Django to detect our Application.

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'articles',
]

Our main Project app urls.py file should look like this :

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

urlpatterns = [
   path('articles/', include('articles.urls')),
]

Let us create a new “urls.py” file inside the “articles” folder.

Make sure the urls.py file looks like this :

from django.urls import path

from . import views

urlpatterns = [
   path('', views.index, name='index'),
]

Here “django.urls” function module is used to specify the path.

Our Project Structure should look like this :

4. Construct views.py file:

Views.py file is responsible for rendering text on the web application.

Now let’s go to views.py and create a function named “index” as mentioned below:

Our views.py file should look like this :

from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello , Welcome to PythonGeeks . We Just Created our first Django App")

We have imported HttpResponse and created a function called “index”, taking an argument – request. This function is also available in the urls.py file.

5. Testing the Application:

Now let’s start our server using the following command :

Python manage.py runserver

Let us navigate to our localhost URL : 127.0.0.1/articles to check if our application is running.

Summary

In this section of the tutorial, we have learned how to create a Django app, we have also learned how to modify urls.py and settings.py files for our Django Project to detect our application routes. This is a simple application. In the upcoming sections of the tutorial we will be learning more about Django Applications.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google | Facebook


Leave a Reply

Your email address will not be published. Required fields are marked *