查看基本.html姜

2024-10-02 16:25:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我在fb或twitter上有通知滚动条。顶菜单.html直接包括在基本.html不幸的是,我只能使用用户方法。是否可以不在我需要通知的每个视图中都写入?我想一次粘贴在一个视图,并一直在顶部菜单.html在基地里!在

from intarface import menu_nots

nots = menu_nots(request)


Tags: 方法用户fromimport视图fb粘贴request
3条回答

我认为使用包含标签来解决您的问题的最佳方法是: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags

@register.inclusion_tag('menu.html')
def show_menu():
    #menu logic2

在基本.html公司名称:

^{pr2}$

包含标记的功能将在每个页面上的每个请求上运行

这是可能的。试试writing your own context processor。在

def add_notifications(request):
    """ Adds Facebook notifications to the view context. """
    return {'notifications': menu_nots(request)}

接下来,将其添加到模板中的“上下文”处理器中设置.py. 在

使用messages%username%!在

视图

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.')

模板

^{pr2}$

相关问题 更多 >