Getting Started with Django: Templates, Static Files, URLs, Views, and More
If you're new to Django, this post will walk you through some of the foundational concepts that are essential to building a Django project—from managing HTML templates and static files to handling routes, dynamic data, and user authentication.
Let’s break everything down step by step.
.
๐งฑ 1. Templates: Where HTML Lives
In Django, if you want to create webpages using HTML, you store your HTML files inside a folder named templates
.
Why?
Django uses its own templating engine to render HTML dynamically. So, keeping your HTML files in a dedicated templates
folder is a best practice.
Example structure:
After this you add templates folder name in Templates oobject in settings.py file like this
๐จ 2. Static Files: CSS, JS, Images
Static files refer to your CSS, JavaScript, images, or any fonts that don’t change frequently.
To manage these in Django, you create a static
folder:
Example structure:
In your HTML file, use Django’s {% load static %}
tag to access them:
๐ผ️ 3. Media Files: User-Uploaded Content
All Static Media files are uploaded by users—like profile pictures, documents, or any other dynamic content.
To store these files, Django uses a media
folder, and you need to configure it in your settings.py
:
⚙️ 4. The settings.py
File
Django's settings.py
is the brain of your project. It controls:
-
Database configuration
-
Installed apps
-
Static and media file paths
-
Middleware
-
Templates configuration
-
Security settings
You’ll often come back to this file when changing configurations or integrating third-party apps.
๐ 5. Running the Django Project
Once your setup is ready and you've created your app, you can start the development server to run your Django project locally.
Use this command:
By default, the server runs at http://127.0.0.1:8000/
— just open it in your browser to see your project live!
๐งช 6. Migrations: Creating Database Tables
To create tables in the database (based on models you define), you use Django’s migration system.
Run this command:
This applies all the built-in or custom model changes to your database. It's like syncing your database with your models.
๐ฎ 7. Creating a Superuser (Admin Panel)
Django has a built-in admin panel. To access it, you need a superuser account.
Create it using this command:
Fill in the username, email, and password. Then you can log into /admin
and manage your data via an intuitive UI.
To go on admin dashboard you write /admin on after IP address then you can see your admin dashboard
๐ 8. Routing in Django
In Django, routing means connecting a URL to a view function.
➕ Static Routes
-
Create a function in
views.py
:
-
Add it in
urls.py
: