Pouria sh
Pouria sh

Reputation: 91

how to show list of categories in navbar using django framework

enter image description here

hello I want to list my categories in navbar from database but the navbar is a partial view and I included my navbar in base.html . how can i give the data to this navbar

this is base.html that i included the partial view:

<body>
   {% include "includes/navbar.html" %}
</body>

this is partial view:

<nav class="navbar navbar-expand-lg navbar-dark ">
    <div class="container-fluid">
        <a class="navbar-brand text-dark" href="#">TechNerd</a>
        <button aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler"
                data-bs-target="#navbarSupportedContent" data-bs-toggle="collapse" type="button">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                <li class="nav-item">
                    <a class="nav-link" href="{% url 'post:posts' %}">Posts</a>
                </li>
                <li class="nav-item dropdown">
                    <a aria-expanded="false" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="#"
                       id="navbarDropdown" role="button">
                        categories
                    </a>
                    <ul aria-labelledby="navbarDropdown" class="dropdown-menu">
                      <li><a class="dropdown-item" href="#">Action</a></li>
                      <li><a class="dropdown-item" href="#">Action</a></li>
                    </ul>
                </li>

            </ul>
        </div>
    </div>
</nav>

Upvotes: 1

Views: 560

Answers (3)

Bernhard Vallant
Bernhard Vallant

Reputation: 50786

You can use a custom context processor to add data to the template context for every request.

# myapp/context_processors.py
from .models import Category

def catgories(request):
    return {
        "categories": Category.objects.all()
    }

# settings.py
TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        # ...your other settings....
        "OPTIONS": {
            "context_processors": (
                "django.contrib.auth.context_processors.auth",
                # ...other processors you use
                "myapp.context_processors.categories",
            ),
        },
    }
]

# my_template.html
{% for category in categories %}
     {{ category.name }}
{% endfor %}

Upvotes: 1

Efraim
Efraim

Reputation: 43

With Context Processors you can pass data to template. So, can use them every .html page.

your_project/context_processors.py

from .models import Category
def category_list(request):
    return {
        "category_urls": [
            {"name": category.name, "url": category.get_absolute_url()}
            for category in Category.objects.all()
        ]
    }

in settings.py

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [str(BASE_DIR / "templates")],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
                "my_app.context_processors. category_list"
            ],
        },
    },
]

in base.html

{% for category in category_urls %}
    
    <a href="{{category.url}}">{{category.name }}</a>
{% endfor %}

Upvotes: 2

Ahmed Almohammed
Ahmed Almohammed

Reputation: 161

You can build your own context processor where it will be run with each request and pass the data from there. You will find a detailed answer in this link (Django variable in base.html).

Upvotes: 2

Related Questions