tamara
tamara

Reputation: 120

Django template not working - not rendering {{ }}

I have followed a tutorial for making a blog engine and successfully integrated it. It is just this template that is not working, I have no idea why. What can be the problem?

Here is the template:

{% extends "base.html" %}

{% block title %}{% post.title %}{% endblock %}

{% block content %}

<h3>{{ post.title }}</h3>
<p>Posted on {{ post.published|date:"F j, Y" }}<p>

{{ post.description|safe }}
<br>
{{ post.body|safe }}
<br>

{% if previous_post %}
<a href="{{ previous_post.get_absolute_url }}" title="{{ previous_post.title }}">
&laquo;&nbsp;Previous Post:&nbsp;&nbsp;{{ previous_post.title }}
</a>{% endif %}

{% if previous_post and next_post %}&nbsp;|&nbsp;{% endif %}

{% if next_post %}
<a href="{{ next_post.get_absolute_url }}" title="{{ next_post.get_absolute_url }}">
Next Post:&nbsp;&nbsp;{{ next_post.title }}&nbsp;&raquo;
</a>
{% endif %}

{% endblock content %}

And here is views.py:

def detail(request, sl):
try:
     post = Post.objects.filter(slug=sl)[0]
     try:
          previous_post = post.get_previous_by_published()
     except:
          previous_post = ""
     try:
          next_post = post.get_next_by_published()
     except:
          next_post = ""
except:
     next_post = ""
     previous_post = ""
     post = ""
return render_to_response('blog/detail.html', {'post':post,
                                        'next_post':next_post,
                                        'previous_post':previous_post,
                                                               },)

Upvotes: 0

Views: 2864

Answers (1)

tamara
tamara

Reputation: 120

Ok so I found out and solved my problem. Just wanted to post it here so someone can use it. It was actually a n00b mistake.

So the {{}} weren't rendering because of the fact that there was nothing in "sl" which I calling upon as argument in the function. It was empty because I was following a tutorial and the tutorial didn't explain a very important thing about Django, and that is that the named groups can be added as arguments in functions, and I had no named group called "sl" in my urlconf in the appropriate place. So by adding this:

(r'^([0-9]{4}/\d{1,2})/(?P<sl>.*)/$', detail),

in the urlconf the problem was solved.

Thanks for all the guidance.

Upvotes: 1

Related Questions