View

incluir variables generales a la plantilla– inclusion tags

  1. #agregar templatetags a APPS de settings
  2. 1.-
  3. apps/templatetags/userInfoTag.py
  4. # -*- coding: utf-8 -*-
  5. from django import template
  6. from django.shortcuts import get_object_or_404
  7. from django.contrib.auth.models import User
  8. register = template.Library()
  9. @register.inclusion_tag(‘admin/users/inclusionTags/userRecordsTags.html’, takes_context=True)
  10. def userRecords(context):
  11.     request = context[‘request’]
  12.     user = get_object_or_404(User, id=request.user.id)
  13.     userName = user.first_name + ‘ ‘ + user.last_name
  14.     return {‘userName’: userName}
  15. 2.-
  16. apps/templatetags/__init__.py
  17. from django.template import add_to_builtins
  18. add_to_builtins(‘templatetags.userInfoTag’)
  19. 3.-
  20. templates/admin/users/inclusionTags/userRecordsTags.html
  21. {{ userName }} # return value of userInfotag.py
  22. 4.-
  23. #se puede usar la plantilla userRecords.html en culaquiera otra plantilla
  24. templates/admin/users/indexSuccess.html
  25. <td>{% userRecords %}</td>  #def userRecords

You may also like