Python /polls/context处的TypeError必须是dict而不是RequestContext
这是django中我的民意测验应用程序的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
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)