[英]Render different templates for ListView
I have a ListView with some good functionality that i want to use from another app.我有一个 ListView,它具有一些我想从另一个应用程序中使用的良好功能。 The way i did it was using
get_template_names
.我这样做的方式是使用
get_template_names
。
def get_template_names(self):
referer = self.request.META['HTTP_REFERER']
if "/mwo/order/" in referer:
return ['invent/use_part.html']
return ['invent/part_list.html']
Which i access from two different apps:我从两个不同的应用程序访问:
path('inventory/', PartListView.as_view(), name='partlist'),
...
path('mwo/order/<int:pk>/add_part/', PartListView.as_view(),
name='add_part'),
But it causes a bug if i use a direct link to 1st url from navbar and not from another app.但是,如果我使用导航栏而不是其他应用程序直接链接到第一个 url,则会导致错误。 Now i'm new to django and i'm pretty sure there should be a better way for this.
现在我是 django 的新手,我很确定应该有更好的方法。 What can i use instead of request referrer to render different template for ListView when i access it from another view.
当我从另一个视图访问 ListView 时,我可以使用什么而不是请求引用来为 ListView 呈现不同的模板。
You can specify the template_name
in the .as_view
:您可以在
.as_view
中指定template_name
:
path('inventory/', PartListView.as_view(
template_name='invent/part_list.html'), name='partlist'),
# …
path('mwo/order/<int:pk>/add_part/', PartListView.as_view(
template_name='invent/use_part.html'), name='add_part'),
Then of course you should remove the get_template_names
from the method, since otherwise you will override that behaviour.然后当然你应该从方法中删除
get_template_names
,否则你将覆盖该行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.