Search code examples
pythondjangolistviewslug

ListView on slug request


My URL

/tags/{slug}   

points to this view:

class TagsDetailList(request, ListView):
    queryset = Link.objects.filter(tags__name__in=['request.slug'])
    template_name = "links/tags_detail_list.html"

So i have the request.slug object in the url. Now i want to make a ListView that filters Link.objects by request.slug and respond to given template with the queried result.

All works but no queries are given on my template.

response template is:

{% extends "base.html" %}
{% block content %}
<h2>Tags Detail List</h2>
    <ul>
        {% if link in object_list %}
            {% for link in object_list %}            
                <li>
                    <a href="{% url 'tag_detail' slug=link.slug %}">{{ link.title }}</a>
                </li>           
            {% endfor %}
        {% else %}
                <p> Error: No Links in queryset! </p>
        {% endif %}
    <ul>
{% endblock %}

i dont get some elements, only the error message. its something bad on the request on my view. who can help me and give me a hint how i can retrieve the request slug on my view?

EDIT:

nice solutions. i learned the way kwargs work(a small part). But on the template i get still the error for no queryset. Tried both answers and also changed a bit but never really worked. Any hint what cause this?


Solution

  • What you've done here doesn't make sense: you're just asking for tags whose names are in the list consisting of the literal text " request.slug".

    You need to override get_queryset so that it queries on the actual value of the slug, which is in self.kwargs.

    def get_queryset(self, *args, **kwargs):
        return  Link.objects.filter(tags__name=self.kwargs ['slug'])
    

    Also, I don't know what that if statement is doing in your template, but you haven't defined "link" do it will never evaluate to true, so no links will show.