Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从django中基于类的视图返回上下文_Python_Django_Django Views - Fatal编程技术网

Python 从django中基于类的视图返回上下文

Python 从django中基于类的视图返回上下文,python,django,django-views,Python,Django,Django Views,因此,如果我使用这样的函数视图: def messageView(request): webpages_list = AccessRecord.objects.order_by('date') date_dict = {'access_records':webpages_list} return render(request,'index.html',context=date_dict) class IndexView(CreateView): form_cla

因此,如果我使用这样的函数视图:

def messageView(request):
    webpages_list = AccessRecord.objects.order_by('date')
    date_dict = {'access_records':webpages_list}
    return render(request,'index.html',context=date_dict)
class IndexView(CreateView):
    form_class = RegisterUserForm 
    template_name = "index.html"

    def index(self,request):
        webpages_list = AccessRecord.objects.order_by('date')
        date_dict = {'access_records':webpages_list}

        return render(request, self.template_name,context=date_dict )
我的url解析器如下所示:
path(“”,views.messageView,name='index'),

一切正常,我的记录按预期显示:

但是,当我尝试这样使用基于类的视图时:

def messageView(request):
    webpages_list = AccessRecord.objects.order_by('date')
    date_dict = {'access_records':webpages_list}
    return render(request,'index.html',context=date_dict)
class IndexView(CreateView):
    form_class = RegisterUserForm 
    template_name = "index.html"

    def index(self,request):
        webpages_list = AccessRecord.objects.order_by('date')
        date_dict = {'access_records':webpages_list}

        return render(request, self.template_name,context=date_dict )
url是这样的:
path(“”,views.IndexView.as_view(),name='index'),

在这种情况下,我什么也得不到,我试着在django online中寻找从类视图返回上下文的简单案例,但没有效果

请问我到底误解了什么?

您可以重写该方法

将其放在基于类的视图中:

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['access_records'] = AccessRecord.objects.order_by('date')
    return context
它返回一个表示模板上下文的字典。

您可以重写该方法

将其放在基于类的视图中:

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['access_records'] = AccessRecord.objects.order_by('date')
    return context

它返回一个表示模板上下文的字典。

如果您只是在学习CBV,请从一个简单的
视图开始,而不是
CreateView
。这是从FBV迁移到CBV的最简单方法:

from django.views import View

class IndexView(View):

    def get(self, request):
        webpages_list = AccessRecord.objects.order_by('date')
        date_dict = {'access_records': webpages_list}
        return render(request, 'index.html', context=date_dict)

    def post(self, request):
        raise NotImplementedError

如果您只是在学习CBV,请从一个简单的
视图开始,而不是
CreateView
。这是从FBV迁移到CBV的最简单方法:

from django.views import View

class IndexView(View):

    def get(self, request):
        webpages_list = AccessRecord.objects.order_by('date')
        date_dict = {'access_records': webpages_list}
        return render(request, 'index.html', context=date_dict)

    def post(self, request):
        raise NotImplementedError

谢谢,这很有效。每次我想以这种方式使用类视图时,我都要这样做吗?或者django是否有一个更简单的内置函数,我没有这个函数?当您使用基于类的视图时,您应该重写
get\u context\u data
方法,以便填充将用作模板上下文的词典。我在回答中为您提供了指向文档的链接,以了解更多详细信息。还有其他方法,比如重写
get
方法并在那里构建上下文dict。每次我想以这种方式使用类视图时,我都要这样做吗?或者django是否有一个更简单的内置函数,我没有这个函数?当您使用基于类的视图时,您应该重写
get\u context\u data
方法,以便填充将用作模板上下文的词典。我在回答中为您提供了指向文档的链接,以了解更多详细信息。还有其他方法,比如重写
get
方法并在那里构建上下文dict。