2016-08-04 26 views
0

我有幾個應用程序的Django項目。爲了與通用名稱(如indexmenu,...,page1page2)更多然後其中一人我採用了這種架構能夠使用模板:Django:如何簡化從子目錄中呈現模板的調用

app1/ 
    templates/ 
     app1/ 
      page1.html 
      page2.html 
app2/ 
    templates/ 
     app2/ 
      page1.html 
      page2.html 

和意見我用它這樣的:

def myview(request): # in app1 
    context={'name':'John', 'surname':'Lennon'} 
    return render(request,"app1/page1.html",context) 

def myview(request): # in app2 
    context={'tool':'hammer', 'size':'big'} 
    return render(request,"app2/page1.html",context) 

它的工作原理,但我寫的應用全稱(app1/app2/)(並且沒有應用程序使用來自其他應用程序的模板或僅從模板/(項目本身除外))和應用程序名稱實際上很長,如10-17個字符(不像app1,app2

問題:有沒有更好的辦法,每個應用程序呈現不會默認爲templates/,而是分別爲templates/app1/,templates/app2/等等?

感謝所有的建議

回答

0

一個簡單的解決方案是,你可以聲明在應用程序的頂部或在settings.py的路徑和使用該變量在整個腳本,例如:

path1 = "app1/templates/" 
path2 = "app2/templates/" 

def myview(request): # in app1 
    context={'name':'John', 'surname':'Lennon'} 
    return render(request,path1+"page1.html",context) 

def myview(request): # in app2 
    context={'tool':'hammer', 'size':'big'} 
    return render(request,path2+"page1.html",context) 

這也可以減少打字工作。

相關問題