Python 查看blog.views.BlogViews没有';t返回HttpResponse对象。它没有返回任何结果

Python 查看blog.views.BlogViews没有';t返回HttpResponse对象。它没有返回任何结果,python,django,Python,Django,我的博客主页的视图有问题。我读过很多有相同错误的问题,但找不到一个我认为与我的问题相匹配的解决方案 我试图用一个新的视图来模拟我的旧代码,以便将来可以进一步定制它 路径(“”,ListView.as_视图( queryset=Post.objects.filter(posted\u date\u lte=now).order\u by(“-posted\u date”)[:25], template_name=“blog/blog.html”), 我以前只使用上面的url模式来显示我的博客文章

我的博客主页的视图有问题。我读过很多有相同错误的问题,但找不到一个我认为与我的问题相匹配的解决方案

我试图用一个新的视图来模拟我的旧代码,以便将来可以进一步定制它

路径(“”,ListView.as_视图( queryset=Post.objects.filter(posted\u date\u lte=now).order\u by(“-posted\u date”)[:25], template_name=“blog/blog.html”), 我以前只使用上面的url模式来显示我的博客文章,但我现在转到一个视图:

def博客视图(TemplateView):
def get(自我,请求):
posts=Post.objects.all().order_by(“-posted_date”)[:25]
args={
"岗位":岗位,,
}
返回呈现(请求'blog/blog.html',args)
以及新的url

path(“”,blogview,name='blog'),
但是,此视图不起作用,并在标题中返回错误。
我相信我正在导入TemplateView、Post和render。TemplateView是一个基于类的视图,因此您可以使用
类对其进行子类化

class BlogViews(TemplateView):
    def get(self, request):
        posts = Post.objects.all().order_by("-posted_date")[:25]
        args = {
            'posts' : posts,
        }
        return render(request, 'blog/blog.html', args)
然后在URL模式中使用
.as_view()

path('', BlogViews.as_view(), name='blog'),
避免覆盖基于类的视图的
get
post
。最终会丢失或复制父类的功能。您可以将您的
列表视图作为视图的起点,例如:

class BlogView(ListView):
    template_name="blog/blog.html"

    def get_queryset(self):
        # Use get_queryset instead of queryset because you want timezone.now()
        # to be called when the view runs, not when the server starts 
        return Post.objects.filter(posted_date__lte=timezone.now()).order_by("-posted_date")[:25]
在您的情况下,使用基于函数的视图可能更容易:

def blog_view(request):
    posts = Post.objects.all().order_by("-posted_date")[:25]
    args = {
        'posts' : posts,
    }
    return render(request, 'blog/blog.html', args)
然后将其包含在URL中,包括:

path('', blog_view, name='blog'),

非常感谢,我现在开始更好地理解基于类的视图了!我最初使用了“.as_view()”部分,并得到了一个“AttributeError:“function”对象没有属性“as_view”错误,但使我的视图成为类修复了该错误。再次感谢。后续的编辑也非常有用。我想知道为什么我的时区现在似乎不正常,但这完全有道理。现在,我们将实施很多这些更改