简体   繁体   中英

view in base.html django

I have notification scrollbar like on fb or twitter. Top menu.html is directly included by base.html Unfortunatelly, I can use only User method there. 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!

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.

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

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

in base.html:

<html>
    ...
    {% show_menu %}
    ...
</html>

function of inclusion tag will run on every request on every pages

Use messages %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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM