joh*_*ees 5 python django django-templates django-models
我有一个模型Category,我希望它的对象始终显示在我的base.html模板的导航菜单中(我的所有其他模板都扩展了该菜单)。
我想学习最佳实践,所以想知道向模板提供这些数据的正确/可接受的方式是什么。
使用自定义上下文处理器:
在 context_processors.py 中:
def categories(request):
return {
'categories': Categories.objects.all()
}
Run Code Online (Sandbox Code Playgroud)
并将其添加到您的 settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
# ... django's default context processors
"your_app.context_processors.categories",
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4522 次 |
| 最近记录: |