Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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>

>Solution :

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 %}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading