bobthemac
bobthemac

Reputation: 1172

Django Python Admin constant side menu

I am building a project using django as it enables me to do more in less time but I have run into an issue I have created a menu as shown in the code below this works if I am on the index page but I get the else part of the if to run if I am on any other page, I think this is because app_list does not exist. What I want to do is be able to do is have a dynamic list of models that are always there in the sidebar so I can get from anywhere to anywhere. Not sure how to go about doing this so it works on every page it is in my base.html file as I want it on every page.

<aside class="sidebar">
    <div id="leftside-navigation" class="nano">
        <ul class="nano-content">
            <li class="active">
                <a href="index.html"><i class="fa fa-dashboard"></i><span>Dashboard</span></a>
            </li>
            {% if app_list %}
                {% for app in app_list %}
                    <li class="sub-menu">
                    {% for model in app.models %}
                        <a href="javascript:void(0);"><i class="fa fa-cogs"></i><span>{{ model.name }}</span><i class="arrow fa fa-angle-right pull-right"></i></a>
                             <ul>
                                {% if model.admin_url %}
                                    <li><a href="{{ model.admin_url }}">View</a>
                                    </li>
                                {% endif %}

                                {% if model.add_url %}
                                    <li><a href="{{ model.add_url }}">{% trans 'Add' %}</a>
                                    </li>
                                {% endif %}
                            </ul>
                        {% endfor %}
                        </li>
                    {% endfor %}
                {% else %}
                        <p>{% trans "You don't have permission to edit anything." %}</p>
                {% endif %}       
         </ul>
     </div>
 </aside>

EDIT:

Ok just to update the question and hopefuly make it more clear what I am looking for is some for of persistent navigation of models like mezzanine offers click the link and you may get a better idea of what I am after doing.

Upvotes: 0

Views: 2110

Answers (1)

allcaps
allcaps

Reputation: 11238

The docs:

Writing your own context processors
A context processor has a very simple interface: It’s just a Python function that takes one argument, an HttpRequest object, and returns a dictionary that gets added to the template context. Each context processor must return a dictionary.

Create a context_processors.py file and add:

def Foo(request):
    return { 'bar': 'Ni!' }

Custom context processors can live anywhere in your code base. All Django cares about is that your custom context processors are pointed-to by your TEMPLATE_CONTEXT_PROCESSORS setting.

In settings.py add your context processor to TEMPLATE_CONTEXT_PROCESSORS:

TEMPLATE_CONTEXT_PROCESSORS += ('context_processors.Foo', )

Than in any template you can do:

{{ bar }}

And it will render as:

Ni!

Upvotes: 2

Related Questions