View: incluir variables generales a la plantilla-- inclusion tags

  1. 9 years ago by macks
    1. #agregar templatetags a APPS de settings
    2.  
    3. 1.-
    4. apps/templatetags/userInfoTag.py
    5.  
    6. # -*- coding: utf-8 -*-
    7. from django import template
    8. from django.shortcuts import get_object_or_404
    9. from django.contrib.auth.models import User
    10.  
    11. register = template.Library()
    12. @register.inclusion_tag('admin/users/inclusionTags/userRecordsTags.html', takes_context=True)
    13. def userRecords(context):
    14.     request = context['request']
    15.     user = get_object_or_404(User, id=request.user.id)
    16.     userName = user.first_name + ' ' + user.last_name
    17.    
    18.     return {'userName': userName}
    19.  
    20. 2.-
    21. apps/templatetags/__init__.py
    22. from django.template import add_to_builtins
    23. add_to_builtins('templatetags.userInfoTag')
    24.  
    25. 3.-
    26. templates/admin/users/inclusionTags/userRecordsTags.html
    27. {{ userName }} # return value of userInfotag.py
    28.  
    29. 4.-
    30.  
    31. #se puede usar la plantilla userRecords.html en culaquiera otra plantilla
    32.  
    33. templates/admin/users/indexSuccess.html
    34. <td>{% userRecords %}</td>  #def userRecords
    Paste this in your website: <script type="text/javascript" src="http://www.posteet.com/embed/2079"></script>

0 comment about "incluir variables generales a la plantilla-- inclusion tags"