Forkstealer
Forkstealer

Reputation: 21

Django context not rendering

I've got a Django template in HTML. I would like to pass a variable to this template using a context. However, when I render the template Django fills the spaces that reference this variable with the string specified by the TEMPLATE_STRING_IF_INVALID setting (I tested this).

Here's the relevant URLconf:

from django.conf.urls import patterns, url

from users import views

urlpatterns = patterns('',
    url(r'^$', views.users),
    url(r'(?P<pk>\d+)/$', views.userdetail),
)

and here's the view it references:

from django.template import RequestContext, loader
...
def userdetail(request, pk):
    user = get_object_or_404(User, pk=pk)
    template = loader.get_template('users/userdetail.html')
    context = RequestContext(request, {'user': user})
    return HttpResponse(template.render(context))

I'm fairly certain it's due to a syntax error in specifying the context but after looking at it for an hour I can't find one. I'm happy to post additional code if you think it may be relevant. Can anyone spot my mistake?

Template for those interested:

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif%}

<h1> You are viewing the page for the individual user {{ user.name }} </h1>

    This user has created the following posts:

    {% for post in user.post_list %}
        <a href="/posts/{{ post.id }}/">{{ post.title }}</a></li>
    {% endfor %}

<p>
Created on {{ user.creation_date }}
</p>

Upvotes: 2

Views: 3912

Answers (1)

The OP wrote:

My supervisor just came around and fixed it really quickly. The issue is that templates have some predefined keywords. User is one of these keywords so django was upset that I was passing it {'user':user} in the context. Changing to {'customuser':user} avoids the collision with the django keyword and fixes this issue.

Upvotes: 2

Related Questions