gpt4 book ai didi

python - 我可以更改基于类的 View 的模板搜索行为吗?

转载 作者:行者123 更新时间:2023-12-01 05:31:42 26 4
gpt4 key购买 nike

我有一个名为交易的应用程序。在这个应用程序中,我有一个名为 BatchFile 的模型。在我的views.py 文件中,我对ListView(以及其他)进行了子类化。默认行为是 django 认为batchfile_list.html 应位于:

templates/transactions/batchfile_list.html

那太好了,但是文件夹变得越来越拥挤。我可以将“templates/transactions/batchfiles”添加到 TEMPLATE_DIRS,但因为默认行为是查找 appname/modelname_type.html,这需要我将模板放入:

templates/transactions/batchfiles/transactions/batchfile_list.html

当我真的希望它出现在:

templates/transactions/batchfiles/batchfile_list.html

或者我的最佳结果:

templates/transactions/batchfiles/list.html  

是否有配置选项可以让我执行此操作?我知道可能没有完全达到最佳结果,但我希望至少能得到稍微不太理想的结果。

谢谢!

最佳答案

最简单的方法是指定template_name:

class MyListView(ListView):
template_name = "transactions/batchfiles/list.html"

另一种方法是覆盖get_template_names:

class MyListView(ListView):

def get_template_names(self):
return ["transactions/batchfiles/list.html"]

请注意,它需要返回可能的模板位置列表。

在这种情况下,只有一个是所需的位置。

关于python - 我可以更改基于类的 View 的模板搜索行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20081980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com