Serving Dropdown from database in every page

django django-models django-templates django-queryset

57 просмотра

1 ответ

I have this project i'm working on and it has 2 apps. Main app: which has the pages(Home, About, Contact) Inventory app: which has the models(Category, Products, Tagging, etc..)

Now the navigation-bar always is always displaying the categories drop-down menu which lists 5 categories or a number of them with a button to see all.

i am using class-based views and i can override the get_context_data method and query the Category model and add it to context on each page render but i was wondering if there is a better method to go about that.

Автор: Basel J. Hamadeh Источник Размещён: 08.11.2019 11:29

Ответы (1)

0 плюса


Have a look at context processors. It can automatically add data to context, and every template will have access to it. You need to edit your to include a path to your custom function.

From the docs:

It’s 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.

Custom context processors can live anywhere in your code base. All Django cares about is that your custom context processors are pointed to by the 'context_processors' option in your TEMPLATES setting — or the context_processors argument of Engine if you’re using it directly.

Автор: NS0 Размещён: 20.08.2016 03:49
Вопросы из категории :