【问题标题】:Django Path to static files changing depending on the page静态文件的 Django 路径根据页面而变化
【发布时间】:2018-02-26 18:49:43
【问题描述】:

我有一个索引页面,其中包含数据表中的运动列表,每一行都有按钮,允许用户按“编辑”进入新页面,在那里他可以查看运动的详细信息。

在索引页面和基本细节移动页面上一切正常,静态文件加载路径为 /gestion_mouvements/static/...

但是当我在我的页面 url 中使用 "(?P[0-9]+)/$" 来获取 url 中的 mouvement ID 时,加载的页面将静态路径更改为 gestion_mouvements/mouvementDetails/static/ ...例如并且无法加载静态文件,因为路径应该是 gestion_mouvements/static/...

我在 settings.py 中检查并尝试了许多不同的静态设置,但到目前为止没有任何效果,有人知道我该如何更改吗?

这是我来自应用程序的 urls.py

from django.conf.urls import url, patterns
from .views import *

urlpatterns = patterns('Gestion_Mouvement.views',
    url(r'^tableau/(?P<idMI>\d+)$','tableau', name = 'tableau'),
    url(r'^$', index),
    url(r'^tableau',tableau, name = 'tableau'),
    url(r'^mouvementDetails/(?P<pk>[0-9]+)/$', 'mouvementDetails', name = 'mouvementDetails'),
    url(r'^index', index, name = 'index'),
    url(r'^refresh_index','refresh_index', name = 'refresh_index'),
    url(r'^finalisation','finalisation', name = 'finalisation'),
    url(r'^creation','creation', name = 'creation'),
    url(r'^historiques','histos', name = 'histos'),
    url(r'^histo-pt0','histosPT0', name = 'histo-pt0'),
    url(r'^histo-entrants','histosEntrants', name = 'histo-entrants'),
    url(r'^histo-sortants','histosSortants', name = 'histo-sortants'),
)

我的 index.html

{% load static %}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Page</title>
<meta name="generator" content="WYSIWYG Web Builder 12 - http://www.wysiwygwebbuilder.com">
<link href="../static\css/Gestion_Mouvements.css" rel="stylesheet">
<link href="../static\css/index.css" rel="stylesheet">
<link href="{% static 'css/jquery-ui.min.css' %}" media="all" rel="stylesheet">
<link href="{% static 'css/jquery.dataTables.min.css' %}" media="all" rel="stylesheet">
<link href="{% static 'css/bootstrap.min.css" rel="stylesheet' %}" media="all">
<link href="{% static 'css//bootstrap-datetimepicker.min.css' %}" media="all" rel="stylesheet">
<script src="{% static 'js/jquery-1.12.4.min.js' %}"></script>
<script src="{% static 'js/jquery.dataTables.min.js' %}"></script>
<script src="{% static 'js/jquery-ui.min.js' %}"></script>
<script type="text/javascript">   
$(document).ready(function() {
 $('#indexTab').dataTable( {
  "bLengthChange": false,
  "lengthMenu": [ [-1], ["All"] ]
} );
 $('.editbtn').click(function(){
        var $row = $(this).closest("tr");    // Find the row
        var $text = $row.find(".idMark").text(); // Find the text
        alert($text);
    });
 $('.deletebtn').click(function(){
       var $row = $(this).closest("tr");    // Find the row
       var $text = $row.find(".idMark").text(); // Find the text
       alert($text);
    });
});
</script>
</head>
<body>
<div id="wb_Shape1" style="position:absolute;left:4px;top:5px;width:1300px;height:100px;z-index:0;">
<img src="../static\images/img0003.png" id="Shape1" alt="" style="width:1300px;height:100px;"></div>
<div id="Html1" style="position:absolute;left:4px;top:107px;width:1300px;height:400px;z-index:1">
<table id="indexTab" class="display" width="100%" cellspacing="0">
            <thead>
            <tr>
                <th>ID</th>
                <th>Date et heure création</th>
                <th>Véhicule</th>
                <th>Pesée 1</th>
                <th>Type mouvement</th>
                <th>Sous type</th>
                <th>Code espèce</th>
                <th>Libellé espèce</th>
                <th>Edit / Delete</th>
            </tr>
        </thead>
        <tbody>     
           {% for mouvement in mouvements %}
            <tr  name="mvtRow{{mouvement.Id}}">
            <th class="idMark">{{mouvement.Id}}</span></th>
            <th class="ce3l7">{{mouvement.DateHeureCreat}}</span></th>
            <th class="ce3l8">{{mouvement.Immat_Transporteur}}</span></th>
            <th class="ce3l9">{{mouvement.Poids_Charge}}</span></th>
            <th class="ce3l2">{{mouvement.Type_Mouvement}}</span></th>
            <th class="ce3l8">{{mouvement.Sous_Domaine}}</span></th>
            <th class="ce3l9">{{mouvement.Espece}}</span></th>
            <th class="ce312">{{mouvement.Libelle}}</span></th>
            <th><a href="/gestion_mouvement/mouvementDetails/{{mouvement.Id}}"><button type="button" class="editbtn">Edit</button> <a href="/gestion_mouvement/tableau"><button class="deletebtn">Delete</button></a></th>
            </tr>
            {% endfor %}
        </tbody>
</table></div>
<input type="text" id="Editbox1" style="position:absolute;left:15px;top:17px;width:900px;height:30px;line-height:30px;z-index:2;" name="ebFluxState" value="{{listOPT.0}}" readonly spellcheck="false">
<input type="submit" id="Button1" name="btn_PeseeSilo" value="Pesée Silo" style="position:absolute;left:936px;top:17px;width:96px;height:40px;z-index:3;">
<input type="submit" id="Button2" name="btn_createManual" value="Création Manuelle" style="position:absolute;left:1037px;top:17px;width:119px;height:40px;z-index:4;">
<input type="submit" id="Button3" name="btn_refresh" value="Rafraichissement" style="position:absolute;left:1174px;top:17px;width:119px;height:40px;z-index:5;">
</body>
</html>

还有我的 settings.py STATIC 选项:

#STATIC_ROOT = os.path.join(os.path.dirname(__file__), '..', 'static').replace('\\', '/')
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
PROJECT_DIR  = os.path.dirname(__file__) 
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")

STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR,'static'),
)

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__),'../templates'),
)

js/css 文件的链接在其他页面中完全相同,它们的调用方式完全相同,但 /mouvementDetails/ 仍会添加到每个路径中

【问题讨论】:

    标签: django python-2.7 django-urls


    【解决方案1】:

    其他一切都已完美设置。您需要更改 HTML 文件的 sn-p,如下所示:

    {% load static %}
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Page</title>
    <meta name="generator" content="WYSIWYG Web Builder 12 - http://www.wysiwygwebbuilder.com">
    <link href="../static\css/Gestion_Mouvements.css" rel="stylesheet">
    <link href="../static\css/index.css" rel="stylesheet">
    <link href="{% static 'css/jquery-ui.min.css' %}" media="all" rel="stylesheet">
    <link href="{% static 'css/jquery.dataTables.min.css' %}" media="all" rel="stylesheet">
    <link href="{% static 'css/bootstrap.min.css" rel="stylesheet' %}" media="all">
    <link href="{% static 'css//bootstrap-datetimepicker.min.css' %}" media="all" rel="stylesheet">
    <script src="{% static 'js/jquery-1.12.4.min.js' %}"></script>
    <script src="{% static 'js/jquery.dataTables.min.js' %}"></script>
    <script src="{% static 'js/jquery-ui.min.js' %}"></script>
    

    收件人:

    {% load static %}
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Page</title>
    <meta name="generator" content="WYSIWYG Web Builder 12 - http://www.wysiwygwebbuilder.com">
    <link href="{% static 'css/Gestion_Mouvements.css' %}"  rel="stylesheet">
    <link href="{% static 'css/index.css' %}" rel="stylesheet">
    <link href="{% static 'css/jquery-ui.min.css' %}" media="all" rel="stylesheet">
    <link href="{% static 'css/jquery.dataTables.min.css' %}" media="all" rel="stylesheet">
    <link href="{% static 'css/bootstrap.min.css" rel="stylesheet' %}" media="all">
    <link href="{% static 'css//bootstrap-datetimepicker.min.css' %}" media="all" rel="stylesheet">
    <script src="{% static 'js/jquery-1.12.4.min.js' %}"></script>
    <script src="{% static 'js/jquery.dataTables.min.js' %}"></script>
    <script src="{% static 'js/jquery-ui.min.js' %}"></script>
    

    因为../static会根据静态文件夹和项目/应用的相对路径搜索所有可能的组合。

    【讨论】:

    • 我理解这个问题,但是这两行是由所见即所得生成的,我可以在 eclipse 上重新编写它们,但我的问题不在索引页面上,它在 mouvementDetails/[ID] 但我确实有同样的问题 au 自动生成的行错过了 django 格式。
    • @Girardclément 在 mouvementDetails/[ID] url,正在渲染哪个模板。可能有些问题。像 {% load static %} 可能没有在那里定义或者这个文件没有在那里扩展。
    • @Girardclément 请展示url mouvementDetails/[ID]的视图和模板代码。
    • 我试过了,但是模板太长了,无法添加。但我试图用 django 调用替换对 js / img / css 的每个调用,它工作得很好,我的问题是现在所见即所得正在生成它自己的代码,它不适用于 Django
    【解决方案2】:

    编辑:嗯,既然你提供了相关信息,问题就清楚了,看cmets。

    更多信息,例如settings.py 中的静态设置会很有帮助。但是,它可能归结为以下几点:

    settings.py 中,请务必将您的STATIC_URL 设置为/static/

    # settings.py
    STATIC_URL = '/static/'
    

    然后,在您的模板中:

    # your_template.html
    {% load static %}
    <img src="{% static "my_app/example.jpg" %}" alt="My image"/>
    

    【讨论】:

    • 我确实有这些设置,它们在我的所有页面上都能完美运行,除了我使用 "(?P[0-9]+)/$" 的那个...
    • 不,你没有。您有两个相对路径 ../static,而不是使用 {% static。问题来了。
    猜你喜欢
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 2019-07-11
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多