2014-11-14 58 views
1

我使用Django 1.7和python 3.4。'函数'对象在Django 1.7中没有属性'META'?

当我去/polls/1/在我的浏览器我得到以下错误:

AttributeError at /polls/1/ 
    "function" object has no attribute "META" 
    Request Method:  GET 
    Request URL:  

    http://localhost:8000/ 



     `enter code here`Django Version:  1.7 


Exception Type: AttributeError 
    Exception Value:  
    "function" object has no attribute "META"' 
    Exception Location: C:\Python34\lib\site-packages\django-1.7-py3.4.egg\django 
    \core\context_processors.py in debug, line 40 
    Python Executable: C:\Python34\python.exe 
    Python Version: 3.4.1 
    Python Path:  

     "['C:\\Users\\Simon\\Desktop\\social network\\mysite', 
    'C:\\Python34\\lib\\site-packages\\django-1.7-py3.4.egg', 
    'C:\\Python34\\lib\\site-packages\\setuptools-5.8-py3.4.egg', 
    'C:\\WINDOWS\\SYSTEM32\\python34.zip', 
    'C:\\Python34\\DLLs', 
    'C:\\Python34\\lib', 
    'C:\\Python34', 
    'C:\\Python34\\lib\\site-packages', 
    'C:\\Python34\\lib\\site-packages\\win32', 
    'C:\\Python34\\lib\\site-packages\\win32\\lib', 
    'C:\\Python34\\lib\\site-packages\\Pythonwin']" 

    Server time: Fri, 14 Nov 2014 14:00:09 +0100 

这里是我的代码:

from django.http import Http404 
from django.http import HttpResponseRedirect, HttpResponse 
from django.core.urlresolvers import reverse 
from django.template import RequestContext, loader 
from django.shortcuts import get_object_or_404, render 
from polls.models import Choice, Question 


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

def detail(request, question_id): 
    try: 
     question = Question.objects.get(pk=question_id) 
    except Question.DoesNotExist: 
     raise Http404 
    return render(render, 'polls/detail.html', {'question': question}) 

def results(request, question_id): 
    question = get_object_or_404(Question, pk=question_id) 
    return render(request, 'polls/results.html', {'question': question}) 

def vote(request, question_id): 
    p = get_object_or_404(Question, pk=question_id) 
    try: 
     selected_choice = p.choice_set.get(pk=request.POST['choice']) 
    except (KeyError, Choice.DoesNotExist): 
      return render(request, 'polls/details.html', { 
      'question': p, 
      'error_message': "You didn't select a choice.", 
     }) 
    else: 
     selected_choice.votes += 1 
     selected_choice.save() 
     return HttpResponseRedirect(reverse('polls:results', args=(p.id,))) 

有什么不对?

回答

16

它在你的detail功能小错字,应该是:

return render(request, 'polls/detail.html', {'question': question}) 

您键入render代替request,并render函数没有属性meta,挑起给定的错误。

+0

谢谢@SébastienD! – 2014-11-14 14:22:57

+0

不客气@NomisPrs,如果有帮助,随时接受答案! – 2014-11-14 15:21:09

+0

事实上,它帮助了,对于这个明显的错误感到抱歉,我应该仔细阅读! – 2014-11-14 15:37:00

相关问题