加载djang中其他静态文件引用的静态文件

2024-06-16 14:04:47 发布

您现在位置:Python中文网/ 问答频道 /正文

在pythondjango项目中,我有一个静态html文件(地图.html). 在这个html文件中引用了其他静态文件。像这样:

<!DOCTYPE html>
<html><head>    
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script>L_PREFER_CANVAS = false; L_NO_TOUCH = false; L_DISABLE_3D = false;</script>
    <script src="mfiles/leaflet_002.js"></script>
    <script src="mfiles/jquery.js"></script>
    <script src="mfiles/bootstrap.js"></script>
    <script src="mfiles/leaflet.js"></script>
    <link rel="stylesheet" href="mfiles/leaflet.css">
    <link rel="stylesheet" href="mfiles/bootstrap.css">

...

静态html文件是从模板文件引用的(索引.html)使用django模板语言:

{% load static %}
<iframe src="{% static "map.html" %}" id="iframemap" style="top:0;left: 0;width:100%;height: 100%; position: absolute; border: none;">

问题是django找不到静态html文件中引用的静态文件(mfiles/slooper_.js等)(地图.html),因为在这个html文件中(地图.html),无法使用django模板语言,因此django找不到这些文件的路径。你知道吗

为了使我的架构更清晰,我提供了以下简单的图表:

django\u模板\u文件----参考--->;地图.html(静态,加载精细)----引用--->;mfiles/*.js等(问题django看不到这些文件)

我怎么能告诉django也在m\u app/static/mfiles/*中查看,而不只是在m\u app/static/中查看?你知道吗

即使m\u app/static/mfiles/中的静态文件仅从m\u app/static/中的其他静态文件引用/(并正确加载)?你知道吗

文件结构的相关部分:

m_project
|______> m_app
    |______> __pychache__
    |______> migrations
    |______> static
            |______> m_files ("this folder is referenced in map.html and django doens't see it")
                |______> leaflet_002.js 
                |______> ...
            |______> map.html ("this is the static html file referenced from template index.html")
    |______> templates
        |______> m_app
            |______> index.html ("this is the template file I am talking about above")
    |______> __init__.py
    |______> admin.py
    |______> apps.py
    |______> models.py
    |______> tests.py
    |______> urls.py
    |______> views.py
|______> m_project
    |______> __pychache__
    |______> __init__.py
    |______> settings.py
    |______> urls.py
    |______> wsgi.py
|______> db.sqlite3
|______> manage.py

Tags: 文件djangopysrc模板falseapphtml
1条回答
网友
1楼 · 发布于 2024-06-16 14:04:47

为什么你的{% load staticfiles %}里没有index.html?你知道吗

# index.html
{% load staticfiles %}
<!DOCTYPE html>
<html><head>
    <script src='{% static "mfiles/leaflet_002.js" %} '></script>
    ...

# django settings
STATICFILES_DIRS = (
    os.path.join(SITE_ROOT, 'static'),
    os.path.join(SITE_ROOT, 'm_app', 'static'),
)

相关问题 更多 >