[Fixed]-Slug in url with CBV in django 1.8

1👍

In the ListView template the list of blog posts will be available as blogpost_list if you don’t set context_object_name.

{% for blogpost in blogpost_list %}
<p><b>{{ blogpost.date|date:"D, d M Y" }}</b></p>
<h4><a href="{% url 'projde:blogdetail' slug=blogpost.slug %}">{{ blogpost.title }}</a></h4>
{% endfor %}

Since you have set context_object_name = 'blog' for your list view, you should change the above for loop to {% for blogpost in blogs %}.

If you still get the error '{'slug': ''}', that suggests that there is a blogpost in your database with slug=''. Fix this through the shell or Django admin, then refresh the page.

In the DetailView template, you don’t need the for loop, and you can access the blog post with {{ blogpost }}.

Leave a comment