GregoryNeal
GregoryNeal

Reputation: 136

Is there any way to load a template dynamically inside of a static navbar template based on the current url?

Hi I'm new to django and I'm trying to make a site with it. My project structure is currently like so:

project/
    manage.py
    project/
        __init__.py
        settings.py
        urls.py
        myapps/
            __init__.py
            main/
                __init__,models,views,urls,etc...
                static/
                    main/
                templates/
                    main/  

I'm using html5boilerplate with all the css, javascript and images are located in

myapps/main/static/main/

And all of my templates reside in

myapps/main/templates/main/

With unique names. When the user hits the main page they load up a slightly modified index.html (renamed to boilerplate.html) that came with html5bp:

<!-- boilerplate.html -->
{% load static %}
...
<body>
    <!--[if lt IE 8]>
        ...
    <![endif]-->

    <!-- Add your site or application content here -->
    {% include "main/index.html" %}

    <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
    <script src="{% static 'main/js/plugins.js' %}"></script>
    <script src="{% static 'main/js/main.js' %}"></script>

    <!-- Google Analytics: change UA-XXXXX-X to be your site's ID. -->
    <script>
        ...
    </script>
</body>
...

I'm using index.html as my static sidebar but I have it in a separate template in case I want to change it down the line and it separates the boring stuff from the fun stuff (who likes looking at bp code?).

<!-- index.html -->
<div id="sidenav" class="sidenav">
    ...
</div>

<div id="main">
    {% include /*SOMETHING HERE*/ %}
</div>

Now whenever a user is redirected to a new page I just want to resolve the destination url and change the include tag to include a completely different template. So I figured I'd have to pass the requested url to a view function and return a template from that, but I am unsure how to do this or if it would work. Sorry if this is a repeat question from somewhere, I did look and couldn't find anything relevant.

Upvotes: 0

Views: 65

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599788

You're thinking about this the wrong way round: you should use template inheritance. Your view should render the specific template for that URL, but each template should in turn extend index.html which provides the sidebar and the rest of the structure.

Upvotes: 0

Related Questions