Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep one menu across multiple Django apps? [closed]

Tags:

python

django

I am looking at creating a django-based website that will be have a few extremely different kinds of sections to it. My thought was that these should be separate apps, however I want these to be able to be brought across to different projects.

I am going to have a main menu system that is created from the database. I want all of these different sections of the site to be under this main menu system, and as such, have a standard header.

Most examples online seem to say that you should create a base.html template with the menu and then have everything else inherit from that. My issue is that this doesn't seem very portable as far as the apps are concerned. I would have to make all of the templates inherit from the base.html, which I feel would reduce their portability?

I guess what I am asking is:

What is the best way to create a persistent main menu system across multiple independent apps in django?

like image 354
Smills Avatar asked Oct 20 '22 02:10

Smills


1 Answers

Persistent Menu System

I think the conventional way of doing this would be to, as you said, create a base template for your project. If you want it to be available to multiple apps the easiest way would probably be to put it in a template folder in the project root, i.e. myproject/templates/myproject/base.html. In this template you build your menu system, and then include a block to be overwritten by the child templates:

{% block content %}{% endblock content %}

Next create a template in your app called something like app_base.html and simply put in the lines:

{% extends "myproject/base.html" %}
{% block content %}{% endblock content %}

At this point your directory structure should look something like this:

myproject/
    myproject/
    app_one/
        templates/
            app_one/
                app_base.html
    templates/
        myproject/
            base.html

Then in every subsequent template you create for that app, at the top add the line:

{% extends "app_one/app_base.html" %}

You may then repeat this for every app you create, and it means you should only have to edit base.html when you want to tweak your menu system.

Portability

The fact that you have created app_base.html in each app should allow you to pluck out any one app and drop it into another project, without having to go through every template and change the name of the template it inherits from. Then you can simply edit app_base.html to make the layout of the pages fall into line with the structure of your new project. This may be done, for example, either by writing the new layout code directly into this template, or by altering the extends line at the top to inherit from the base template for your new site (note that this base template will have to include a content block).

Note: When you add the templates directory to your project root don't forget to add the line:

TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]

to settings.py so that Django knows where to find the additional templates.

I hope this helps. It was the best solution I could think of, but I feel that it may be difficult to achieve 100% autonomy with template inheritance in Django, as there is a requirement for child and parent templates to maintain consistent blocks with one another.

like image 157
Apoc Avatar answered Oct 31 '22 19:10

Apoc