Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.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 /polls/context处的TypeError必须是dict而不是RequestContext_Python_Django_Pycharm - Fatal编程技术网

Python /polls/context处的TypeError必须是dict而不是RequestContext

Python /polls/context处的TypeError必须是dict而不是RequestContext,python,django,pycharm,Python,Django,Pycharm,这是django中我的民意测验应用程序的view.py选项卡。我无法调试错误:Type error:context必须是dict而不是RequestContext。它取自YouTube频道“TheCodex”上“我的第一个django应用程序”的第6个视频。请帮忙 from django.shortcuts import render from django.http import HttpResponse from django.template import loader, Request

这是django中我的民意测验应用程序的
view.py
选项卡。我无法调试错误:
Type error:context必须是dict而不是RequestContext。
它取自YouTube频道“TheCodex”上“我的第一个django应用程序”的第6个视频。请帮忙

from django.shortcuts import render

from django.http import HttpResponse
from django.template import loader, RequestContext
from .models import Question

def index(request):
    latest_questions = Question.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = RequestContext(request, {
        'latest_questions': latest_questions
    })
    return HttpResponse(template.render(context))

def detail(request, question_id):
    return HttpResponse("This is the detail view of the question: %s" %question_id)

def results(request, question_id):
    return HttpResponse("These are the results of the question: %s" %question_id)

def vote(request, question_id):
    return HttpResponse("Vote on question: %s" %question_id)
但是

好多了


该视频的用法很旧,如果你是新手,我建议你直接学习django 1.11。django 1.11的文档是。

我使用的是相同的教程系列,也有相同的问题。 而不是像这样使用RequestContext

 context = RequestContext(request,{
    'latest_questions': latest_questions
})
改用这本字典

context = {
    'latest_questions': latest_questions,
}

我是从Django文档中得到的。

试试这个而不是你的

def index(request):
    latest_questions = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_questions': latest_questions}
    return render(request, 'polls/index.html', context)

在发布之前,您是否费心阅读Django版本的官方文档?您可能想解释一下这解决问题的原因:
context = {
    'latest_questions': latest_questions,
}
def index(request):
    latest_questions = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_questions': latest_questions}
    return render(request, 'polls/index.html', context)