Building a Custom Django Project/ Application(Folders and Files 2)

Building a Custom Django Project/ Application(Folders and Files 2)

Introduction

What is a project or application that is not custom-made and per the intent of the developer or client?

A dream project or application is primarily engineered from the thought or what can be called inspiration.

In addition, this article will convey custom-made Django folders and files, the continuation of The Django Project Folder and Files Structure and Architecture 1.

Building Custom Django Application

Create an application URL file

Firstly, a urls.py file needs to be created in the Django application my_app because it is not always provided by Django(default).

The code below gives the file its full potential apart from its name; without it, the Django server will throw an error. The code below helps to initiate the process of creating the URL pattern of the application.

#the application url file
from django.urls import path

urlpatterns = [

]

Connect the application URL file to the project URL file.

In the project folder, my_project there is a URL file that needs to connect with the URL file of all the project's applications.

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


urlpatterns = [
    path('admin/', admin.site.urls),
#added the application url as the project base url pattern
    path('', include('my_app.urls')), 
]

include needs to be imported, and can be called the connection conjunction key in this case.

Connect the app to the project.

All Django applications need to be connected to the project, done in the settings file. settings.py

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

    # put the name of the application
    'my_app',
]

Create our first Django web page.

We will override the default Django web page to a simple custom-based one. These changes will be made through the application views.py and urls.py files.

#the application urls.py file
from django.urls import path
#import everything in the application views file
from my_app.views import *

urlpatterns = [
    #base url pattern connected to customHomePage view
    #you can name a url path using the python keyword name
    path('', customHomePage, name='custom_home_page')
]
  • Django custom view using HttpResponse

    The view returns an HttpResponse object that contains the generated response. Each view function is responsible for returning an HttpResponse object.

#the application views.py file
from django.shortcuts import render
#import HttpResponse
from django.http import HttpResponse

#a simple function-based function
def customHomePage(request):
    #HttpResonse is to render contents/ strings on the page
    return HttpResponse('My Django application custom home page')
  • Django custom view using Template

    This requires creating an HTML template file and connecting to the view to render content on the web page.

def customHomePage(request):
    # return render(request, template_name='customHomePage.html')
    return render(request, 'customHomePage.html')

Your Django server http://127.0.0.1:8000/ should display the page shown below.

I hope you created your Django custom-made project with less difficulty and ease.