[英]view in base.html django
I have notification scrollbar like on fb or twitter. 我有通知滚动条,如fb或twitter。 Top menu.html is directly included by base.html Unfortunatelly, I can use only User method there. 顶部menu.html直接由base.html包含。不幸的是,我只能在其中使用User方法。 Is it possible to not write in every view that I need notifications? 是否有可能不在我需要通知的每个视图中都写? I want to once paste in one view and have it always in top menu.html which is in base! 我想一次粘贴到一个视图中,并始终将其放在顶部的menu.html中!
from intarface import menu_nots
nots = menu_nots(request)
It is possible. 有可能的。 Try writing your own context processor . 尝试编写自己的上下文处理器 。
def add_notifications(request):
""" Adds Facebook notifications to the view context. """
return {'notifications': menu_nots(request)}
Next, add it to your TEMPLATE_CONTEXT_PROCESSORS in settings.py. 接下来,将其添加到settings.py中的TEMPLATE_CONTEXT_PROCESSORS。
I think that the best way to solve your problem by using a inclusion tags: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags 我认为通过使用包含标签来解决问题的最好方法是: https : //docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags
@register.inclusion_tag('menu.html')
def show_menu():
#menu logic2
in base.html: 在base.html中:
<html>
...
{% show_menu %}
...
</html>
function of inclusion tag will run on every request on every pages 包含标记功能将在每个页面的每个请求上运行
Use messages %username% ! 使用消息 %username%!
view 视图
from django.contrib import messages
messages.add_message(request, messages.INFO, 'Hello world.')
messages.debug(request, '%s SQL statements were executed.' % count)
messages.info(request, 'Three credits remain in your account.')
messages.success(request, 'Profile details updated.')
messages.warning(request, 'Your account expires in three days.')
messages.error(request, 'Document deleted.')
template 模板
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
添加到TEMPLATE的setting.py部分中,追加到context_processors
列表-> app_name.view.base,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.