在django-apps中查找静态文件和模板的顺序 [英] Order of finding static files and templates in django-apps

查看:233
本文介绍了在django-apps中查找静态文件和模板的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我的django项目中有2个应用程序,其模板和静态文件具有相同的子路径:

  app1 / 
static /
style.css
templates /
index.html
app2 /
static /
style.css
templates /
index.html

比在settings.py中添加了这两个应用程序:

  INSTALLED_APPS =(
'app1',
'app2',

现在我以某种方式在模板中使用'style.css'和'index.html',例如

  {%include'index.html'%} 

所以问题是:



Django是否保证当我引用'style.css'或'index.html'将使用来自app2子目录的文件?



在这种情况下,Django是否有其他方式指定文件的最佳变体?

解决方案

根据文档,首次比赛胜利:


重复的文件名由默认解决方式与
模板解析的工作原理相似:首先在
指定的位置之一找到的文件将被使用。


https:/ /docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#django-admin-collectstatic



现在,当你告诉Django收集静态文件或渲染模板,Django询问特殊的查找器,以便在您的配置中为指定的资源定义。



静态文件的默认顺序是FileSystemFinder STATICFILES_DIRS ,以便添加它们。如果FileSystemFinder找不到这些目录中的文件,Django将使用下一个查找器集合,AppDirectoriesFinder在您的应用程序目录中的静态子目录中进行搜索。



应用于模板。当你告诉Django渲染index.html时,它首先要求在code> TEMPLATE_DIRS 中定义的目录中找到名为filesystem.Loader的模板。如果搜索失败,Django会询问下一个模板加载器app_directories.Loader,它在应用程序templatessubdirs中搜索模板目录。



为了回答您的问题,因为app1已注册在app2之前,Django会使用它的style.css和index.html,而不是来自app2的。如果你想改变这种行为,把app2放在你安装的应用程序设置之上。



文档:
https://docs.djangoproject.com/en/dev/ref/templates/api /#django.template.loaders.app_directories.Loader


For example I have 2 apps in my django project with templates and static files with identical subpath:

app1 / 
    static /
        style.css
    templates /
        index.html
app2 / 
    static /
        style.css
    templates /
        index.html

than, in settings.py I added this two apps:

INSTALLED_APPS = (
    'app1',
    'app2',
)

now I use in some way 'style.css' and 'index.html' in templates, e.g.

{% include 'index.html' %}

so, question is:

Does Django guarantee that when I reference to 'style.css' or 'index.html' will be used files from app2 subdirectories?

Is it any other way to point Django preferable variants of files in such situation?

解决方案

As per documentation, first match wins:

Duplicate file names are by default resolved in a similar way to how template resolution works: the file that is first found in one of the specified locations will be used.

https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#django-admin-collectstatic

Now, when you tell Django to collect static files or render templates, Django asks special "finders" in order they are defined in your configuraiton for specified resource.

Default order for static files is "FileSystemFinder" that searches STATICFILES_DIRS in order they are added in it. If FileSystemFinder fails to find file in those dirs, Django uses next finder set, the "AppDirectoriesFinder" that searches in "static" subdirectories in your apps directories.

Same mechanic is applied to templates. When you tell Django to render "index.html", it first asks "filesystem.Loader" find template named like that in directories defined in TEMPLATE_DIRS. If search fails, Django asks next template loader, "app_directories.Loader" that searches template dirs in applications "templates" subdirs.

To answer your question, because app1 is registered before app2, Django will use it's style.css and index.html instead of ones coming from app2. If you want to change this behaviour, put app2 above app1 in your installed apps setting.

Documentation: https://docs.djangoproject.com/en/dev/ref/templates/api/#django.template.loaders.app_directories.Loader

这篇关于在django-apps中查找静态文件和模板的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆