django
  1. django-shortcuts

Advanced Django Concepts Shortcuts

Below are the shortcuts for advanced Django concepts that will help you write better, efficient, and cleaner code.

Shortcut for Using Static Files in Templates

{% load static %}

Example:

{% load static %}
<!DOCTYPE html>
<html>
<head>
    <title>My Static Files Example</title>
    <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">
</head>
<body>
    <h1>Hello World!</h1>
</body>
</html>

Output:

<!DOCTYPE html>
<html>
<head>
    <title>My Static Files Example</title>
    <link rel="stylesheet" type="text/css" href="/static/css/style.css">
</head>
<body>
    <h1>Hello World!</h1>
</body>
</html>

Explanation:

This shortcut is used for loading static files (CSS, JavaScript, images) in Django templates. By using this shortcut, the static files are easily accessible and render properly.

Use:

Add this shortcut at the top of Django templates to use static files.

Important Points:

  • The static files should be placed in the "static" folder in the app directory.
  • The app should be added in the INSTALLED_APPS list in the settings.py file.

Summary:

{% load static %} is a shortcut used for loading static files (CSS, JavaScript, images) in Django templates. The static files should be placed in the "static" folder in the app directory, and the app should be added in the INSTALLED_APPS list in the settings.py file.

Published on: