Django 渲染指定目录下不存在的模板
Django renders a template that does not exist in specified directory
我有模型 'Video' 并且我有这个模型的列表视图,然后在另一个应用程序中我试图创建一个视图 'UserVideosListView' 供专用用户查看。当我在为此视图制作任何模板之前在浏览器中打开页面时,我能够看到当前用户的视频(不是所有视频)。
# inside 'users' app
class UserVideosListView(ListView):
model = Video
template_name = "users/user_videos.html"
context_object_name = 'videos'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['videos'] = Video.objects.filter(author=self.request.user)
return context
# inside 'videos' app
class VideoListView(ListView):
model = Video
paginate_by = 25
template_name = 'videos/video_list.html'
context_object_name = 'videos'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['videos'] = Video.published_videos.all()
return context
P.S。我确定我输入的 URL 是最新的,'users' 目录
中没有模板
Django 呈现 videos/video_list.html 因为 ListView
基于 MultipleObjectTemplateResponseMixin
和 BaseListView
:
class ListView(MultipleObjectTemplateResponseMixin, BaseListView):
"""
Render some list of objects, set by `self.model` or `self.queryset`.
`self.queryset` can actually be any iterable of items, not just a queryset.
"""
Within MultipleObjectTemplateResponseMixin
(GitHub),模板名称也是根据model.
生成的
# If the list is a queryset, we'll invent a template name based on the
# app and model name. This name gets put at the end of the template
# name list so that user-supplied names override the automatically-
# generated ones.
if hasattr(self.object_list, 'model'):
opts = self.object_list.model._meta
names.append("%s/%s%s.html" % (opts.app_label, opts.model_name, self.template_name_suffix))
Django docs says MultipleObjectTemplateResponseMixin
:
template_name_suffix
The suffix to append to the auto-generated candidate template name. Default suffix is _list.
Returns a list of candidate template names. Returns the following list:
- the value of template_name on the view (if provided)
- <app_label>/<model_name><template_name_suffix>.html
我建议你覆盖 get_queryset()
方法而不是 get_context_data()
class UserVideosListView(ListView):
model = Video
context_object_name = 'videos'
...
def get_queryset(self):
return <b>super().get_queryset().filter(author=self.request.user))</b>
我有模型 'Video' 并且我有这个模型的列表视图,然后在另一个应用程序中我试图创建一个视图 'UserVideosListView' 供专用用户查看。当我在为此视图制作任何模板之前在浏览器中打开页面时,我能够看到当前用户的视频(不是所有视频)。
# inside 'users' app
class UserVideosListView(ListView):
model = Video
template_name = "users/user_videos.html"
context_object_name = 'videos'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['videos'] = Video.objects.filter(author=self.request.user)
return context
# inside 'videos' app
class VideoListView(ListView):
model = Video
paginate_by = 25
template_name = 'videos/video_list.html'
context_object_name = 'videos'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['videos'] = Video.published_videos.all()
return context
P.S。我确定我输入的 URL 是最新的,'users' 目录
中没有模板Django 呈现 videos/video_list.html 因为 ListView
基于 MultipleObjectTemplateResponseMixin
和 BaseListView
:
class ListView(MultipleObjectTemplateResponseMixin, BaseListView): """ Render some list of objects, set by `self.model` or `self.queryset`. `self.queryset` can actually be any iterable of items, not just a queryset. """
Within MultipleObjectTemplateResponseMixin
(GitHub),模板名称也是根据model.
# If the list is a queryset, we'll invent a template name based on the # app and model name. This name gets put at the end of the template # name list so that user-supplied names override the automatically- # generated ones. if hasattr(self.object_list, 'model'): opts = self.object_list.model._meta names.append("%s/%s%s.html" % (opts.app_label, opts.model_name, self.template_name_suffix))
Django docs says MultipleObjectTemplateResponseMixin
:
template_name_suffix
The suffix to append to the auto-generated candidate template name. Default suffix is _list.
Returns a list of candidate template names. Returns the following list:
- the value of template_name on the view (if provided)
- <app_label>/<model_name><template_name_suffix>.html
我建议你覆盖 get_queryset()
方法而不是 get_context_data()
class UserVideosListView(ListView):
model = Video
context_object_name = 'videos'
...
def get_queryset(self):
return <b>super().get_queryset().filter(author=self.request.user))</b>