@register.simple_tag
def optional_logout(request, user):
 try:
  logout_url = reverse(u'rest_framework:logout')
 except NoReverseMatch:
  snippet = format_html(u'<li class="navbar-text">{user}</li>', user=escape(user))
  return mark_safe(snippet)
 snippet = u'<li class="dropdown">\n        <a href="#" class="dropdown-toggle" data-toggle="dropdown">\n            {user}\n            <b class="caret"></b>\n        </a>\n        <ul class="dropdown-menu">\n            <li><a href=\'{href}?next={next}\'>Log out</a></li>\n        </ul>\n    </li>'
 snippet = format_html(snippet, user=escape(user), href=logout_url, next=escape(request.path))
 return mark_safe(snippet)

@ register . simple_tag def optional_logout ( request , user ) : try : logout_url = reverse ( u'rest_framework:logout' ) except NoReverseMatch : snippet = format_html ( u'<li class="navbar-text">{user}</li>' , user = escape ( user ) ) return mark_safe ( snippet ) snippet = u'<li class="dropdown">\n        <a href="#" class="dropdown-toggle" data-toggle="dropdown">\n            {user}\n            <b class="caret"></b>\n        </a>\n        <ul class="dropdown-menu">\n            <li><a href=\'{href}?next={next}\'>Log out</a></li>\n        </ul>\n    </li>' snippet = format_html ( snippet , user = escape ( user ) , href = logout_url , next = escape ( request . path ) ) return mark_safe ( snippet )
include a logout snippet if rest frameworks logout view is in the urlconf .