Saturday, July 31, 2021

how to setup template folder for Django development?

 there are two ways to setup the template folder for django development.

first we can setup the path that Django framework can automatically recognize it..

under the component folder, we have to create the a template folder, then create a folder with name matching the component name.

for example we have a lead component, then the structure of the project should look like as following








in the view.py, we can implement with the code snippet below

from django.shortcuts import render

# Create your views here.

def home_page(request):
    return render(request"leads/home_page.html")

second. we can modify the DIRS attribute in  TEMPLATES setting section in the settings.json to map to the templates path.

TEMPLATES = [
    {
        'BACKEND''django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS'True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

then we only have to create a templates folder under the current component folder. we can add the template html in the template folder.







in the view.py we use the template page directly.

def home_page(request):
    return render(request"home_page.html")








No comments:

Post a Comment