Djang入门三 - template and static files查找机制

This is for Django Beginners

Template Lookup Mechanism(模版查找机制)

Templates Directories

Django looks for templates in the directories specified in the TEMPLATES setting in file myproject/settings.py. The default configuration typically includes(根据配置TEMPLATES查找):

  • Any directories you specify in the DIRS option of the TEMPLATES setting.

  • The templates directory within each installed app(if APP_DIRS is set to True).

    Example configuration:

      TEMPLATES = [
          {
              'BACKEND': 'django.template.backends.django.DjangoTemplates',
              'DIRS': ['templates'],  # Add your templates directory here
              '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',
                  ],
              },
          },
      ]
    
Lookup Order
  1. Directories specified in DIRS.(首先查找配置在DIRS中的目录)
  2. templates/ folder of each installed app, in the order the apps are registered in INSTALLED_APPS. So it’s suggested to use format app_name/template_name.html in templates/ folder. (在各installed app的templates目录下查找,如果同名,按照app在INSTALLED_APPS中的顺序查找.因此, 在 templates/中建议使用app_name/template_name.html)

If it finds multiple templates with the same name in different directories, it uses the first one it encounters based on the lookup order.(如果在不同目录下找到多个同名templates,返回找到的第一个)

Recommandation for Template Names
  1. For templates of app, it’s recommanded to place it under project/app/templates/app/base.html.
  2. For templates of project, it’s recommanded to place it under project/templates/base.html

Use the path relative to templates in views:

# Render project template
return render(request, 'base.html')
# Render app template
return render(request, 'app/base.html')

Static File Lookup Mechanism(模版查找机制)

Static Files Directories

Django looks for static files in the directories specified in the STATIC_URL and STATICFILES_DIRS setting in file myproject/settings.py. (根据配置STATIC_URLSTATICFILES_DIRS查找)

  • Ensure django.contrib.staticfiles is configured in INSTALLED_APP.

  • STATIC_URL: The URL prefix for accessing static files (e.g., static/).

  • STATICFILES_DIRS: A list of additional directories where Django will look for static files, beyond the default app-specific directories.

    Example configuration:

      STATIC_URL = 'static/'
      STATICFILES_DIRS = [
          BASE_DIR / "static",
      ]
    
Lookup Order
  1. Directories specified in STATICFILES_DIRS.
  2. static directory within each installed app, in the order the apps are registered in INSTALLED_APPS.(Django automatically includes these directories in the static file lookup)

If it finds multiple static files with the same name in different directories, it uses the first one it encounters based on the lookup order.(如果在不同目录下找到多个同名templates,返回找到的第一个).

Debug:

% python manage.py findstatic css/style.css
Found 'css/style.css' here:
myproject/static/css/style.css
myproject/myapp/static/css/style.css
myproject/myapp2/static/css/style.css
Recommandation for static file name
  1. For static files of app, it’s recommanded to place it under project/app/static/app/css/style.css or project/app/static/app/js/main.js.
  2. For static files of project, it’s recommanded to place it under project/static/css/style.css or project/static/js/main.js

Use the path relative to static in templates:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello world </title>
    <!-- static files in app -->
    <link rel="stylesheet" href="{% static 'app/css/style.css' %}">
    <script type='text/javascript' src="{% static 'app/js/main.js' %}"></script>
    <!-- static files in project -->
    <link rel="stylesheet" href="{% static 'css/style.css' %}">
    <script type='text/javascript' src="{% static 'js/main.js' %}"></script>  
</head>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值