Access objects globally in templates in Django 1.6 -


I want a menu on all my pages in Django 1.6.

In the menu, I also want to include some items from my database. I know that I can expand the list view and get the items, and I know that I Can I set the variable in get_context_data , but how can I make objects on all my pages? I think it would be stupid to set context_data in every single scene.

The result of my menu should be something like this:

  Home User User 1 User 2 User 3 Help Questions Question 1 Question 2 About Question 3  < / pre> 

What would be a good way to do this? Middlewares? Template tag?

You are looking for a custom:

You should write this way Work:

  #file: my_context_processors.py def menu_data (requested): Return {'menu': calculate_menu (requested), # & lt; - The menu has been added to your reference # for all templates   

where calculate_menu returns the object with an object and subtitles (dictionary?) In addition, you have other information in path , user and request .

Then include your custom processor on the App SettingsTemplate Processor processor:

  TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth", "django.core. context_processors.debug ", #" django.core.context_processors.i18n "," django.core.context_processors.media "," django.core context_processors.static "," django.contrib.messages.context_processors.messages "," django core.context_processors.csrf ", 'django.c ore.context_processors.request', 'my_app.my_context_processors.menu_data', # & lt; ---- here)   

At this time, Menu item is available

Do not worry about words, it is not difficult and this is the right way.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -