2017-09-18 101 views
1

我有mouvements列表的索引頁在數據表上,每行有按鈕,允許用戶按「編輯」去一個新的頁面,在那裏他可以查看破壞的細節。Django的路徑靜態文件改變取決於頁

在索引頁上的基本細節解放運動網頁一切正常工作,靜態文件加載路徑爲/ gestion_mouvements /靜態/ ...

但是當我使用「(?P [0-9] +)/ $「在我的網頁url中能夠獲得URL中的mouvement ID,所加載的頁面將靜態路徑更改爲gestion_mouvements/mouvementDetails/static/...例如,並且無法加載靜態文件,因爲路徑應該是gestion_mouvements /靜態/ ...

我檢查和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/get添加到每個路徑

回答

0

其他的事情都是完美的。您需要更改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> 

要:

{% 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搜索基於靜態文件夾的相對路徑上的所有可能的組合和項目/應用程序。

+0

我明白這個問題,但是這兩個線通過所見即所得的產生,我可以重新把它們寫在食,但我的問題是不是索引頁,它是在mouvementDetails/[ID],但我有AU汽車同樣的問題生成的行錯過了django格式。 –

+0

@Girardclément在** mouvementDetails/[ID] ** url中,哪個模板正在呈現。可能有一些問題。像** {%load static%} **可能沒有在那裏定義,或者這個文件沒有在那裏擴展。 –

+0

@Girardclément請顯示url ** mouvementDetails/[ID] **的視圖和模板代碼。 –

0

編輯:好了,現在你所提供的相關信息,這個問題是清楚的,看評論。

一些更多的信息例如您在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"/> 
+0

我確實有這些設置,他們對我的所有網頁完美,execpt的一個地方,我使用「(?P [0-9] +)/ $」 ...... –

+0

不,你不知道。你有兩個相對路徑「../ static」,而不是使用「{%static」。有問題。 – olieidel