qq_43112062
北冥有魚焉~
采纳率100%
2020-07-27 17:21 阅读 98

请求大佬支援!Django中使用BaseDetailView报错。

环境:Python3.8 + Django3.0

urls.py

path('detail/<int:pk>/', views.BaseDetailView.as_view())

views.py

from django.http import JsonResponse
from .models import Blog
from django.views.generic.detail import BaseDetailView

class BlogDetailView(BaseDetailView):
    model = Blog

    def render_to_response(self, context):
        blog = context['object']
        data = {
            'blog': {
                'id': blog.id,
                'title': blog.title,
                'content': blog.content,
                'author': {
                    'id': blog.author.id,
                    'username': blog.author.username
                }
            }
        }
        return JsonResponse(data)

请求URL:

http://127.0.0.1:8000/blog/detail/1/

报错:

ImproperlyConfigured at /blog/detail/1/

BaseDetailView is missing a QuerySet. Define BaseDetailView.model, BaseDetailView.queryset, or override BaseDetailView.get_queryset().
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    qq_43656607 放风喽 2020-07-27 20:11

    BaseDetailView缺少QuerySet。定义BaseDetailView.model, BaseDetailView.queryset,或覆盖BaseDetailView.get_queryset().

    所以你可以给quertset赋值
    queryset = 某个model.objects.all()

    或者重写get_queryset方法,
    def get_queryset(self):
    return 某个model.objects.all()

    学学djangorestfrmawork吧

    点赞 评论 复制链接分享

相关推荐