Question

I have a Django application with following model:

class Topic(models.Model):
    title = models.CharField(max_length=140)

There is a URL, which should display Topic's details:

urlpatterns = patterns('',
    [...]
    (r'^topic/(\d+)$', 'history_site.views.topic_details'),
    [...]
)

history_site.views.topic_details is defined as

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 has following content:

<!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>

When I try to access the URL http://127.0.0.1:8000/topic/1 I get the error 'str' object has no attribute 'META'.

Why?

How can I fix it?

Was it helpful?

Solution

Looking at the doc

TemplateResponse.__init__(request, template, context=None, content_type=None, status=None, current_app=None)

the first parameter TemplateResponse takes is a request not a template name

so your code is wrong, try to change it for something like:

return TemplateResponse(request, 'topic.tpl.html', locals())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top