我的views.py中的代码在仍然不是模板格式时工作正常
def detail(request, entity_group_id):
entity_group = get_object_or_404(EntityGroup, pk=entity_group_id) # noqa
entity_list = entity_group.members.order_by('name')
context = {
'entity_group': entity_group,
'entity_list': entity_list,
}
return render(request, 'core/detail.html', context)
当我将其更改为Generic View时,会出现NoReverseMatch ......
class DetailView(generic.DetailView):
model = EntityGroup
template_name = 'core/detail.html'
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
context['entity_group'] = EntityGroup
context['entity_list'] = EntityGroup.members
return context
这是我的models.py
class Entity(models.Model):
name = models.CharField(max_length=30)
class EntityGroup(models.Model):
name = models.CharField(max_length=20)
members = models.ManyToManyField(Entity, through='Membership')
class Membership(models.Model):
entity_group = models.ForeignKey(EntityGroup, on_delete=models.PROTECT, null=False)
entity = models.ForeignKey(Entity, on_delete=models.PROTECT, null=False)
如何在通用视图上设置反转?
这是urls.py
from django.urls import path
from . import views
app_name = 'core'
urlpatterns = [
# # path('', views.index, name='index'),
# path('<int:entity_group_id>/', views.detail, name='detail'),
# path('<int:entity_group_id>/results/', views.results, name='results'),
# path('<int:entity_group_id>/vote/', views.vote, name='vote'),
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:entity_group_id>/vote/', views.vote, name='vote'),
]
这是要求的回溯:
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
158. response = self.process_exception_by_middleware(e, request)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
156. response = response.render()
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\response.py" in render
106. self.content = self.rendered_content
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\response.py" in rendered_content
83. content = template.render(context, self._request)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\backends\django.py" in render
61. return self.template.render(context)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py" in render
175. return self._render(context)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py" in _render
167. return self.nodelist.render(context)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py" in render
943. bit = node.render_annotated(context)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\loader_tags.py" in render
155. return compiled_parent._render(context)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py" in _render
167. return self.nodelist.render(context)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py" in render
943. bit = node.render_annotated(context)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\loader_tags.py" in render
67. result = block.nodelist.render(context)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py" in render
943. bit = node.render_annotated(context)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py" in render_annotated
910. return self.render(context)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\defaulttags.py" in render
447. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\base.py" in reverse
88. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Users\acer-1\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
632. raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /core/2/
Exception Value: Reverse for 'vote' with arguments '(None,)' not found. 1 pattern(s) tried: ['core\\/(?P<entity_group_id>[0-9]+)\\/vote\\/$']
我有一个投票视图,可以在更改模板中的详细信息视图之前正常工作,这就是为什么我没有在此处发布。
答案 0 :(得分:1)
在原始视图中,您明确地将EntityGroup
对象实例传递给模板。
entity_group = get_object_or_404(EntityGroup, pk=entity_group_id)
context = {
'entity_group': entity_group
但是在你的新DetailView
中,你传递的是EntityGroup
类,而不是对象实例。
context['entity_group'] = EntityGroup
因此,当模板尝试根据无法查找的ID呈现URL时,可能会触发NoReverseMatch
。
在新的DetailView
中,尝试传递对象实例。
context['entity_group'] = self.get_object()