在/ app / category / Python /中配置不正确

时间:2018-01-08 01:20:30

标签: python django

我想制作一个显示POST'模型的页面。每个类别都会显示内容。例如,当我在详细信息中放入Python链接时,只有POST的模型'带有Python类别的内容显示在category.html中。当我在class.html中放入Python链接时,我收到错误,在/ app / category / Python / CategoryView中的ImproperlyConfigured缺少QuerySet。定义CategoryView.model,CategoryView.queryset或覆盖CategoryView.get_queryset()。我在views.py

中编写了代码
def top(request):
        content = POST.objects.order_by('-created_at')[:5]
        category_content = Category.objects.order_by('-created_at')[:5]
        page = _get_page(blog_content, request.GET.get('page'))

        return render(request, 'top.html',{'content':content,'category_content':category_content,"page":page})

   class CategoryView(BaseListView):
     template_name = 'category.html'

     def get_queryset(self):
        category_name = self.kwargs['category']
        self.category = Category.objects.get(name=category_name)
        queryset = super().get_queryset().filter(category=self.category)
        return queryset

     def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['category'] = self.category
        return context
在urls.py中

urlpatterns = [
    path('top/', views.top, name='top'),
    path('category/<str:category>/',views.CategoryView.as_view(), name='category'),
]

在models.py

class Category(models.Model):
    name = models.CharField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

class POST(models.Model):
    title = models.CharField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

    def __str__(self):
        return self.title
top.html中的

<div class="list-group">
            <a href="#">
              Category
            </a>
                   <div>
                         {% for category in category_content %}
                            <a href="{% url 'category' category.name %}">
                            {{ category.name }}
                            </a>
                         {% endfor %}
                   </div>
</div>
category.html中的

{% load static %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Category</title>
</head>
<body>

    <div>
                     {% for content in queryset %}
                        <h2>{{ content.title }}</h2>
                        <img src="content.image.url" />
                        <a class="btn btn-outline-primary btn-lg btn-block" href="{% url 'detail' content.pk %}">SHOW DETAIL</a>
                     {% endfor %}
    </div>

          <div>
            <a href="#" class="list-group-item active">
              Category
            </a>
                   <div>
                         {% for category in category_content %}
                            <a class="list-group-item justify-content-between" href="{% url 'category' category.name %}">
                            {{ category.name }}
                            </a>
                         {% endfor %}
                   </div>
        </div>

</body>
</html>

我真的不明白为什么ImproperlyConfigured这意味着settings.py是错误的。当我将BaseListView更改为ListView时,会发生同样的错误。我在CategoryView的类中写了get_queryset,所以我无法理解代码需要QuerySet。我该如何解决这个问题?我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

一个问题是,您正在调用super().get_queryset() expects your class to define a queryset or a model。添加model = Category将解决该错误。

看起来你只返回一个只有一个条目的查询集。如果您只想获得一个类别,那么使用DetailView

会更简单
from django.views.generic import DetailView
from .models import Category  # or wherever it is


class CategoryView(DetailView):
    model = Category
    template_name = 'category.html'

    def get_object(self):
        category_name = self.kwargs['category']
        return Category.objects.get(name=category_name)

请注意,您的模板需要更新为引用categoryobject,而不是循环查询集。