[Python] Template extends base.html but doesn't get the content of the sidebar

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have base_planner.html that contains a navbar and a sidebar. When I extend this base in any other template, both the navbar and sidebar are inherited, but the sidebar doesn't have any content that must be there. And I'm not getting any errors. The only template that has the sidebar content is the home page, which is merely a ListView html page (projectsmodel_list.html) containing one row: {% extends 'app_planner\base_planner.html' %}

    Here is my views.py (I want TestView to inherit the sidebar content):

    class ProjectListView(LoginRequiredMixin, generic.edit.FormMixin, generic.ListView):
    context_object_name = 'project_list'
    model = ProjectsModel
    form_class = ProjectsForm
    success_url = reverse_lazy('app_planner:planner_home')

    def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['create_project_form'] = ProjectsForm()
    return context

    def form_valid(self, form):
    project_name = form.cleaned_data['project_name']
    project_author = self.request.user
    create_project_form_item = ProjectsModel(project_name=project_name, project_author=project_author)
    create_project_form_item.save()
    return super().form_valid(form)

    def form_invalid(self, form):
    return super().form_invalid(form)

    def post(self, request, *args, **kwargs):
    self.object_list = self.get_queryset()
    form = self.get_form()
    if form.is_valid():
    return self.form_valid(form)
    else:
    return self.form_invalid(form)


    class TestView(generic.TemplateView):
    template_name = 'app_planner/test.html'


    Here is my urls.py:

    app_name = 'app_planner'

    urlpatterns = [
    path('', views.ProjectListView.as_view(), name='planner_home'),
    path('test/', views.TestView.as_view(), name='test'),
    ]


    Here is the body of my base_planner.html:

    {% if user.is_authenticated %}
    <nav id='navbar' class="navbar navbar-expand-lg bg-white">
    <div class="container-fluid">
    <a id='logo' class="navbar-brand" href="{% url 'app_planner:planner_home' %}"><h3><b><span style="color:#FC5E5E;">Savvy</span> Planner</b></h3></a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
    <div class="navbar-nav">
    <div class="navbar-center">
    <a id='projects_board' class="nav-link {% if request.resolver_match.view_name == 'app_planner:planner_home' %}active{% endif %}" href="{% url 'app_planner:planner_home' %}">Projects & Tasks</a>
    <a id='analytics' class="nav-link {% if request.resolver_match.view_name == 'app_planner:analytics' %}active{% endif %}" href="#">Analytics</a>
    <a id='analytics' class="nav-link {% if request.resolver_match.view_name == 'app_planner:test' %}active{% endif %}" href="{% url 'app_planner:test' %}">Test</a>
    </div>
    <div class="navbar-right">
    <a id='notifications' class="nav-link" href="#"><img src="{% static 'notifications_empty.svg' %}"></a>
    <a id='logout' class="nav-link" href="{% url 'app_planner:logout' %}" class="btn btn-simple"><img src="{% static 'logout.svg' %}" width="20" height="20"></a>
    </div>
    </div>
    </div>
    </div>
    </nav>

    <div class="sidebar">
    <div class='row' style="font-family: 'Nunito', sans-serif; padding-bottom: 25px;">
    <div class='col-sm-6' style="height: 60px; width: 60px; border-radius: 16px; background-color: #FC5E5E;"></div>
    <div class="col-sm-6" style="width: fit-content;">
    <p><span style='font-size:25px'><b>Awesome Team</span><br>
    <span style='color:#888B94'>Workspace</span></b></p>
    </div>
    </div>

    <div class='sidebar_projects' style="border-top: solid 2px #E3E4E9; margin-left: -10px;">
    <br>
    <form method="POST">
    {% csrf_token %}
    <div class='row project_form'>
    {% for field in create_project_form %}
    <div class='col-sm-4' style='align-items:center; display: flex;'>{{ field.label }}</div>
    <div class='col-sm-4'>{{ field }}</div>
    <div class='col-sm-4' style='align-items:center; display: flex;'><button id='add_project' type="submit" style='background:white;border:0px;'><img src="{% static 'plus_button.svg' %}"></button></div>
    {% endfor %}
    </div>
    </form>

    <div class="row">
    {% for project in project_list %}
    <div class='col-sm-12 single_project'><a href="{% url 'app_planner:project_detail' pk=project.pk %}"><span style='color:rgba(255, 255, 255, 0);'>__</span>{{ project.project_name }}</a></div>
    {% endfor %}
    </div>
    </div>
    </div>

    <div class='content'>
    {% block content %}
    {% endblock %}
    </div>
    {% endif %}


    Here is my test.html:

    {% extends 'app_planner/base_planner.html' %}
    {% block content %}
    <h1>Check the sidebar</h1>
    {% endblock %}


    I've tried changing the templates, adding content of ProjectListView to other views, and creating a separate sidebar.html and using {% include 'app_planner/sidebar.html %} in {% block sidebar %} in base_planner.html calling {% super.sidebar %} in other templates, but nothing worked. I keep seeing an empty sidebar. This is my first time facing such an issue as {% extend %} has always worked correctly for me. I can't understand where the mistake is.

    Continue reading...

Compartilhe esta Página