2

I want to decouple my big Django app into multiple apps. There is a problem however: navigation. Each application should be able to add its own menu items, leaving other apps' navigation visible.

I don't think it is possible with simple template extending with blocks. A possible solution could be a separate navigation app where every other app can register its menu items. I don't want to create menu items manually via the admin interface. I want something like this:

# in users app:
import navapp
navapp.register(view_name='users_summary', path=('Home', 'Users'))
navapp.register(view_name='user_search', path=('Search', 'User'))
...

# in another app:
import navapp
navapp.register(view_name='other_stuff', path=('Home', 'Other stuff'))
navapp.register(view_name='about', path=('About',)
...

In base.html: import a templatetag from navapp and use it somehow to render menu.

Of course, my example is simplified (e.g., I purposely omitted ordering of menu items, though it is important), but I hope it conveys the idea.

Is there already a reusable app for this purpose? If not, what is the best approach to implement it?


I have found a similar question: http://answers.splunk.com/answers/125820/single-navigation-for-multiple-apps

What I want is similar to django cms menu integration but I don't want to mess with django-cms. I need a little app instead.

utapyngo
  • 6,946
  • 3
  • 44
  • 65
  • “I don't want to create menu items manually via the admin interface.” — care to share why? I'm interested in your rationale because this question is relevant to one of my current projects. – Anton Strogonoff Jun 08 '14 at 07:12
  • Because each app knows what menu items it needs. Whe should I add them manually? – utapyngo Jun 08 '14 at 12:33
  • Thanks. I'm using another approach: my apps apps don't know anything about site menu. IMO navigation is better be handled by a central authority in a single place to keep it simple. But I like the idea of registering items from code. Currently I have [an app](https://github.com/strogonoff/django-site-menu) that allows managing menu from admin and it can be inconvenient, fact. (BTW thanks for fixing links in my other question.) – Anton Strogonoff Jun 09 '14 at 04:08

1 Answers1

1

My solution was to add an installed_apps context_preprocessor:

from django.conf import settings

def installed_apps(request):
    return {'INSTALLED_APPS': settings.INSTALLED_APPS}


TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    "baseapp.context_processors.installed_apps",
    ...
)

Then in the template I can add a nav element if the app is in INSTALLED_APPS

{% if 'app' in INSTALLED_APPS %}
  <li><a href="/app">APP</a></li>
{% endif %}
Chris Montanaro
  • 16,948
  • 4
  • 20
  • 29