Setting Up a Django Project Structure

Creating the Project and Appplication

# Generate the Django project structure named 'tpdemo'
django-admin startproject tpdemo
cd tpdemo

# Create a new application within the project
python manage.py startapp myapp

# Establish template directories
mkdir templates
templates/myapp

Database Configuration in settings.py

Modify tpdemo/tpdemo/settings.py to configure database connectivity:

# Allow all host addresses
ALLOWED_HOSTS = ['*']

# Register the custom application
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
]

# Define template directory locations
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(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',
            ],
        },
    },
]

# MySQL database connection parameters
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mytest',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

MySQL Driver Installation

pip install mysqlclient

Main URL Routing Configuration

Configure primary routing in tpdemo/tpdemo/urls.py:

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
]

Application-Level URL Configuration

Create tpdemo/myapp/urls.py with the following content:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.homepage, name="homepage"),
    
    # Testing routes - optional
    path('syntax-demo', views.syntax_demo, name="syntax_demo"),
    path('inheritance-demo', views.inheritance_demo, name="inheritance_demo"),
    
    # City cascading operations
    path('location-display/', views.location_display, name='location_display'),
    path('location-data/<int:parent_id>', views.location_data, name='location_data'),
]

View Implementation

Define view funcsions in tpdemo/myapp/views.py:

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from datetime import datetime
from myapp.models import District


def homepage(request):
    return render(request, "myapp/index.html")


def syntax_demo(request):
    context = {}
    context['username'] = "JohnDoe"
    context['numbers'] = [15, 25, 35]
    context['student'] = {"fullname": "Jane Smith", "years": 22}
    
    sample_data = [
        {"fullname": "Alex Chen", "gender": 1, "years": 35, 'status': 0},
        {"fullname": "Maria Garcia", "gender": 0, "years": 33, 'status': 2},
        {"fullname": "Sam Wilson", "gender": 1, "years": 25, 'status': 1},
        {"fullname": "Lisa Brown", "gender": 0, "years": 23, 'status': 1},
    ]
    
    context['dataset'] = sample_data
    context['current_time'] = datetime.now
    context['value_one'] = 150
    context['value_two'] = 30
    
    return render(request, "myapp/demo1.html", context)


def inheritance_demo(request):
    return render(request, "myapp/demo2.html")


def location_display(request):
    return render(request, "myapp/district.html")


def location_data(request, parent_id=0):
    locations = District.objects.filter(parent_id=parent_id)
    location_list = []
    
    for item in locations:
        location_list.append({'identifier': item.id, 'location_name': item.name})
    
    return JsonResponse({'locations': location_list})

Template Creation

Create the main template at tpdemo/templates/myapp/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Django Template Layer</title>
</head>
<body>
    <h2>Django Template Layer Implementation</h2>
    <ul>
        <li><a href="{% url 'syntax_demo' %}">1. Template Syntax Examples</a></li>
        <li><a href="{% url 'inheritance_demo' %}">2. Template Inheritance</a></li>
        <li><a href="{% url 'location_display' %}">3. Ajax Implementation - Location Cascading</a></li>
    </ul>
</body>
</html>

Server Execusion

python manage.py runserver 0.0.0.0:8000

Tags: Django web-framework python backend-development database-configuration

Posted on Fri, 29 May 2026 21:39:51 +0000 by Drezard