【发布时间】:2016-12-10 18:44:27
【问题描述】:
我有几个应用程序的 django 项目。为了能够在其中一个以上使用具有通用名称的模板(如index、menu、...、page1、page2),我采用了这种模式:
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/等等?
感谢所有建议
【问题讨论】:
标签: django templates django-templates