2
我有以下模型Django應用程序:如何解決「'str'對象沒有屬性'META'」的錯誤?
class Topic(models.Model):
title = models.CharField(max_length=140)
有一個URL,它應該顯示Topic
的細節:
urlpatterns = patterns('',
[...]
(r'^topic/(\d+)$', 'history_site.views.topic_details'),
[...]
)
history_site.views.topic_details
被定義爲
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.loader import get_template
from django.template import Context, RequestContext
from django.views.decorators.csrf import csrf_protect
import logging
from opinions.models import Topic
from django.template.response import TemplateResponse
logging.basicConfig(filename='history-site.log',level=logging.DEBUG)
def topic_details(request, topic_id_string):
topic_id = int(topic_id_string)
topic = Topic.objects.get(id=topic_id)
return TemplateResponse('topic.tpl.html', locals())
topic.tpl.html
有以下內容:
<!DOCTYPE html>
{% block prehtml %}
{% endblock %}
<html>
<head>
<title>{% block title %}{% endblock %}</title>
{% block scripts %}{% endblock %}
</head>
<body>
<h1>{{ topic.title }} </h1>
{% block content %}
{% endblock %}
</body>
</html>
當我嘗試訪問URL http://127.0.0.1:8000/topic/1
時,出現錯誤'str' object has no attribute 'META'
。
爲什麼?
我該如何解決?
你能包括錯誤的代碼嗎? –
@VictorCastilloTorres你可以在這裏找到回溯 - http://dpaste.com/1257647/。 –
我確實提出了一個解決問題的答案:D –