In this section, we will explore how Django handles templates and static files. Templates are used to generate dynamic HTML content, while static files include CSS, JavaScript, and images that are served as-is to the client.
Key Concepts
-
Templates:
- Templates are HTML files that can include dynamic content using Django Template Language (DTL).
- They allow you to separate the presentation layer from the business logic.
-
Static Files:
- Static files are assets like CSS, JavaScript, and images.
- They are served directly to the client without any processing.
Templates
Creating a Template
-
Directory Structure:
- Create a directory named
templatesinside your app directory. - Inside the
templatesdirectory, create a subdirectory with the same name as your app.
myapp/ templates/ myapp/ index.html - Create a directory named
-
Writing a Template:
- Create an HTML file (e.g.,
index.html) inside thetemplates/myappdirectory.
<!-- templates/myapp/index.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>My Django App</title> </head> <body> <h1>Welcome to {{ app_name }}!</h1> </body> </html> - Create an HTML file (e.g.,
-
Rendering a Template in a View:
- Use the
renderfunction to render the template in your view.
# views.py from django.shortcuts import render def index(request): context = {'app_name': 'My Django App'} return render(request, 'myapp/index.html', context) - Use the
Template Inheritance
-
Base Template:
- Create a base template that other templates can extend.
<!-- templates/myapp/base.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %}My Django App{% endblock %}</title> </head> <body> <header> <h1>My Django App</h1> </header> <main> {% block content %}{% endblock %} </main> <footer> <p>© 2023 My Django App</p> </footer> </body> </html> -
Extending the Base Template:
- Extend the base template in other templates.
<!-- templates/myapp/index.html --> {% extends "myapp/base.html" %} {% block title %}Home{% endblock %} {% block content %} <h2>Welcome to {{ app_name }}!</h2> {% endblock %}
Static Files
Configuring Static Files
-
Settings:
- Add the
STATIC_URLandSTATICFILES_DIRSsettings in yoursettings.pyfile.
# settings.py STATIC_URL = '/static/' STATICFILES_DIRS = [ BASE_DIR / "static", ] - Add the
-
Directory Structure:
- Create a
staticdirectory at the project level.
myproject/ static/ css/ styles.css js/ scripts.js images/ logo.png - Create a
Using Static Files in Templates
-
Loading Static Files:
- Use the
{% load static %}tag to load static files in your templates.
<!-- templates/myapp/index.html --> {% extends "myapp/base.html" %} {% load static %} {% block title %}Home{% endblock %} {% block content %} <h2>Welcome to {{ app_name }}!</h2> <link rel="stylesheet" href="{% static 'css/styles.css' %}"> <img src="{% static 'images/logo.png' %}" alt="Logo"> {% endblock %} - Use the
Practical Exercise
Exercise: Create a Template and Use Static Files
-
Task:
- Create a new template named
about.htmlthat extends thebase.htmltemplate. - Add a CSS file to style the
about.htmlpage. - Include an image on the
about.htmlpage.
- Create a new template named
-
Steps:
- Create
about.htmlin thetemplates/myappdirectory. - Add a CSS file in the
static/cssdirectory. - Add an image in the
static/imagesdirectory. - Update the
about.htmltemplate to use the CSS file and display the image.
- Create
-
Solution:
<!-- templates/myapp/about.html --> {% extends "myapp/base.html" %} {% load static %} {% block title %}About{% endblock %} {% block content %} <h2>About Us</h2> <link rel="stylesheet" href="{% static 'css/styles.css' %}"> <img src="{% static 'images/logo.png' %}" alt="Logo"> <p>This is the about page of My Django App.</p> {% endblock %}/* static/css/styles.css */ body { font-family: Arial, sans-serif; } h2 { color: #333; } p { font-size: 1.2em; }
Summary
In this section, we covered the basics of using templates and static files in Django. We learned how to create and render templates, use template inheritance, and include static files like CSS, JavaScript, and images in our templates. These concepts are fundamental for building the front-end of your Django applications. In the next section, we will dive into models and databases, which are crucial for handling data in Django.
Django Web Development Course
Module 1: Introduction to Django
- What is Django?
- Setting Up the Development Environment
- Creating Your First Django Project
- Understanding Django Project Structure
Module 2: Django Basics
- Django Apps and Project Structure
- URL Routing and Views
- Templates and Static Files
- Models and Databases
- Django Admin Interface
Module 3: Intermediate Django
Module 4: Advanced Django
- Advanced Querying with Django ORM
- Custom User Models
- Django Signals
- Testing in Django
- Performance Optimization
