2016-08-04 41 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) 

这也可以减少打字工作。