Python错误:对象没有属性“META”

2024-04-25 20:46:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我对我的观点有点问题,因为它在django上返回了一个错误,但我不知道我做错了什么。在我看来,我的代码如下:

    from django.views.generic import TemplateView
    from django.shortcuts import render

    from community.models import Community


    class CommunityLanding(TemplateView):

        def get_context_data(request):

            template_name = 'community/landing.html'

            objects = Community.objects.all()

            context = {
                'object': objects
            }

            return render(request, template_name, context)

有人能给我指出正确的方向吗?你知道吗


Tags: django代码namefromcommunityimportobjectsrequest
1条回答
网友
1楼 · 发布于 2024-04-25 20:46:31

你的代码几乎都是错的。template\u name属性是在类中定义的,而不是在get\u context\u data方法中定义的。get\u context\u data方法只接受一个参数,它是“self”变量,应该只返回上下文。您不需要手动呈现模板,只要定义了模板名称,其他方法就会处理此问题。你知道吗

from django.views.generic import TemplateView
from community.models import Community

class CommunityLanding(TemplateView):
    template_name = 'community/landing.html'

    def get_context_data(self):
        context = super().get_context_data()
        objects = Community.objects.all()
        context['object'] = objects
        return context

你应该读更多关于subclassing the generic views

相关问题 更多 >

    热门问题