gpt4 book ai didi

python - ListView 缺少 QuerySet。定义 ListView.model、ListView.queryset 或覆盖 ListView.get_queryset()

转载 作者:太空宇宙 更新时间:2023-11-04 04:18:48 27 4
gpt4 key购买 nike

我真的不明白是什么导致了错误我检查了文档,这里有一个非常相似的例子是我的 views.py,我使用的应用程序下的 urls.py 包含,以及模板

View .py

class SchoolListView(ListView):
context_object_name = 'schools'
model = models.School

网址.py

from django.urls import path
from . import views

#My name space
app_name = 'basicapp'
urlpatterns = [
path('', views.ListView.as_view(), name='list'),
path('details', views.DetailView.as_view(), name='details')
]

和我的模板

{% extends 'basicapp/basicapp_base.html'%}
{% block body_block %}
<div class="jumbotron">
<h1>Welcome to list of all schools</h1>
<ol>
{% for school in schools %}
<h2><li><a href="{{school.id}}">{{school.name}}</a></li></h2>
{% endfor %}
</ol>

{% endblock %}

我得到了这个我不太明白的错误

Exception Type: ImproperlyConfigured
Exception Value:
ListView is missing a QuerySet. Define ListView.model, ListView.queryset, or override ListView.get_queryset().


Traceback Switch to copy-and-paste view
C:\ProgramData\Miniconda3\lib\site-packages\django\core\handlers\exception.py in inner
response = get_response(request) ...
▶ Local vars
C:\ProgramData\Miniconda3\lib\site-packages\django\core\handlers\base.py in _get_response
response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
C:\ProgramData\Miniconda3\lib\site-packages\django\core\handlers\base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
C:\ProgramData\Miniconda3\lib\site-packages\django\views\generic\base.py in view
return self.dispatch(request, *args, **kwargs) ...
▶ Local vars
C:\ProgramData\Miniconda3\lib\site-packages\django\views\generic\base.py in dispatch
return handler(request, *args, **kwargs) ...
▶ Local vars
C:\ProgramData\Miniconda3\lib\site-packages\django\views\generic\list.py in get
self.object_list = self.get_queryset() ...
▶ Local vars
C:\ProgramData\Miniconda3\lib\site-packages\django\views\generic\list.py in get_queryset
'cls': self.__class__.__name__ ...
▶ Local vars

最佳答案

您的 urls.py 中有错误,您没有引用 SchoolListView,而是引用通用 ListView 本身。您可以通过以下方式解决此问题:

# app/urls.py

from django.urls import path
from . import views

#My name space
app_name = 'basicapp'

urlpatterns = [
# SchoolListView instead of ListView
path('', views.<b>SchoolListListView</b>.as_view(), name='list'),
# <i>probably</i> SchoolDetailView instead of DetailView, and with a pk in the url
path('details', views.DetailView.as_view(), name='details')
]

由于您在 views.py 中导入了 ListView,解释器在使用 views.ListView 时不会出错,您只需“重新- 在您的 views.py 中导出“ListView”。

可能您还定义了 SchoolDetailView 而不是 DetailView,并且 URL 可能应该包含您要显示其详细信息的学校的主键,但您确实没有提供足够的代码来解决该问题。

关于python - ListView 缺少 QuerySet。定义 ListView.model、ListView.queryset 或覆盖 ListView.get_queryset(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54893530/

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