Getting started with Django App

Getting started with Django App

A step-by-step guide to setting up Views and URLs.

Introduction

An app in Django is a self-contained module that provides a specific functionality within the Django project. A Django project can have multiple apps, each of which handles multiple functionalities of a project.

For example, let's say you create a blog Django project and you want to have the following functionalities for your blog project

  • User registration and authentication to log in to the blog

  • Handling the posts in the blog

  • Handling the comments of a blog post

  • Handling the tag section

An app is created for each of the above functionalities and is linked to the Django blog project.

Setting up a Django app

  • After creating the Django project called the project, execute the command below

      python manage.py startapp app
    

    This command will create the Django app called app and its related files in your Django project and the file structure will be as below

  • To link this app to the project, you need to add it to the INSTALLED_APPS list in the settings.py file

      #project/settings.py
    
      INSTALLED_APPS = [
          #...
          'app.apps.AppConfig',
          #...
      ]
    

    By this, Django will include the app whenever the project is started.

  • Now you should include the URLs of app in the urls.py file of the project

      #project/urls.py
    
      from django.urls import path,include
    
      urlpatterns = [
          path("app/",include('app.urls')),
      ]
    

    This tells Django to route requests starting with '/app/' to the app.

Understanding the relationship between views.py and urls.py in a Django App

In Django, the views.py file of an app contains the functions that define the logic for your app. These functions take an HTTP request as input, process and do the required transformation on the request and return an HTTP response.

The urls.py of an app contains the URL patterns for your app, which map specific URLs to their corresponding functions in the views.py.

Let's see how views.py and urls.py work together:

views.py:

#app/views.py

from django.http import HttpResponse

# Create your views here.
def home(request):
    return HttpResponse("This is the homepage of the app!")

urls.py:

NOTE: Create a urls.py file in the app folder.

#app/urls.py

from django.urls import path
from app import views

urlpatterns = [
    path("",views.home,name='app_home'),
]

In the above example 'home' function of views.py returns an HTTP response stating "This is the homepage of the app!". In urls.py there is a URL pattern that maps the root URL of the app ('app/' as stated in project/urls.py) to the home function in views.py. The name parameter is optional and can be used while generating URLs in templates.

When the user visits the root URL of your app (http://127.0.0.1:8000/app/) Django will use the URL pattern defined in urls.py of your app to find the corresponding view function and returns the response as performed by the view function. In this case, Django will call the home function which will return an HTTP response with the message "This is the homepage of the app!".

Like this, you can include multiple apps in the project and specify their URLs and their corresponding view functions and perform specific tasks when a user visits the URL.

Conclusion

In this article, we have learned about Django App and how to set it up, including the understanding of views.py, and urls.py, and how they work together when the app-specific URL is called.

Thank you for reading the article! I hope you find it informative and useful.