Stimo
Stimo

Reputation: 23

populate bootstrap dropdown on the base.html using django

Im using django 1.9 and python 3.5 which i'm both really new too, and i'm having trouble populating a bootstrap dropdown which is located in the base.html. So far i have this:

base.html:

<li class="dropdown">
       <a href="#" class="dropdown-toggle" data-toggle="dropdown">Events
       <b class="caret"></b></a>
           <ul class="dropdown-menu">
               {% if categories %}
                   {% for cat in category %}
                       <li><a href="{% url 'categories' pk=cat.pk %}">
                       {{ cat.name }}</a></li>
                   {% endfor %}
               {% else %}
                       There are no categories present.
               {% endif %}
           </ul>
</li>

views.py:

def categories(request, pk):
    category = Category.objects.get(pk=pk)
    return render(request, 'categories.html', {'category': category})

urls.py:

url(r'^categories/(?P<pk>\d+)/$', views.categories, name='categories'),

So i want the drop down to display the available categories from the database and then when i click on one it will obviously load up the categories.html displaying the relevant category.

any help would be much appreciated.

Edit: sorry i forgot to say the problem i am having. Im not getting the drop down populated, and is only giving me the "there is no categories present"

Upvotes: 1

Views: 1306

Answers (2)

Curtis Olson
Curtis Olson

Reputation: 967

There are a couple of issues here:

Firstly, you don't have any context in your view called categories, yet you're checking for them in your template. Your view function is called 'categories', which might be creating some confusion for you. However, this is not context that is accessible to your view. It's just a function.

Secondly, you're not getting a list of categories (which you could iterate as you are in your template) in your view, you're getting a single category with:

category = Category.objects.get(pk=pk)
# using the get() method means you're requesting a single object

So you need to do something more like:

categories = Category.objects.all()
# here, we're getting a QuerySet (list of objects), rather a single object

Then add the categories to your context. So your view would end up looking like this:

def categories(request, pk):
    categories = Category.objects.all()
    return render(request, 'categories.html', {'categories': categories})

Also, you'll need to change your iterator to iterate over categories, not category:

{% for cat in categories %}
    <li><a href="{% url 'categories' pk=cat.pk %}">
    {{ cat.name }}</a></li>
{% endfor %}

Upvotes: 1

comonadd
comonadd

Reputation: 1988

So, "categories" variable will never give you "true", while you do not define it, and add it to template context.

Do this in your python code

def categories(request, pk):
    categories = Category.objects.all()

    return render(request, 'categories.html', {'categories': categories})

Upvotes: 0

Related Questions