I would like to create the links of the menu dynamically based on the Category models. I've used DetailView and ListView for create a list and detail page of a single category and it run fine.
Now I would like to see in base.html a new link in a "dropdown" menu every time the user add a new Category.
This is base.html
{% load static %}
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>{% block head_title %}Test{% endblock head_title %}</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'list_tag' %}">Tag</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Category
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
{% for category in category_list %}
<a href="{{ category.get_absolute_url }}">{{ category.category_name }}</a>
{% endfor %}
</div>
</li>
</div>
</nav>
<div class="container">
<!-- INIZIO corpo centrale -->
{% block content %}
{% endblock content %}
<!-- FINE corpo centrale -->
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>
This is list_category.html
{% extends 'base.html' %}
{% load static %}
{% block head_title %}Elenco Categorie | {{ block.super }}{% endblock head_title %}
{% block content %}
{% for category in category_list %}
<h1><a href="{{ category.get_absolute_url }}">{{ category.category_name }}</a></h1>
<hr>
{% endfor %}
{% endblock content %}
This is views.py
class CategoryList(ListView):
model = Category
context_object_name = 'category_list'
template_name = "list_category.html"
class SingleCategory(DetailView):
model = Category
template_name = "single_category.html"
When I use the code in base.html it was shown an empty menu. How I can resolve?
UPDATE:
Using the solution of @ans2human in this post, I've created a file context_processors.py like below:
from .models import Category
def blog_menu(request):
link_menu = Category.objects.all()
return {
'link_menu': link_menu
}
Then I've added the string in settings.py:
'blog.context_processors.blog_menu',
The new strings of dropdown menu are these:
<li class="nav-item active dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="far fa-newspaper" id="img"></i> Blog
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#"></i> {{ link_menu }}</a>
</div>
</li>
Now I've this situation in the menu
It seems that work but not correctly. How I can resolve?
6: user_input = int(input()) if user_input == 1: print(Executing menu item 1) elif user_input == 2: print(Executing menu item 2) elif user_input == 3: print(Executing menu item 3) elif user_input == 4: print(Executing menu item 4) elif user_input == 5: print(Executing menu item 5) elif user_input == 6: print(Exiting... ...
dynamic-django-forms is a simple, reusable app that allows you to build (and respond to) dynamic forms, i.e. forms that have variable numbers and types of fields. A few examples of uses include: Building and sending out surveys. Job applications where each job might have a different application forms.
UPDATE 2
Good news! Since I'm not a django expert, I had a trivial difficulty. By reasoning a little more, I found the solution.
Below the solution:
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
{% for link in link_menu %}
<a class="dropdown-item" href="{{ link.get_absolute_url }}"> {{ link.category_name }}</a>
{% endfor %}
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With