Django: adding new admin_tags


/ Published in: Django
Save to your folder(s)

First: create a file under djangoapp/templatetags where you define your tags [e.g.: myadmin_tags.py]:

Second: create the html snippets that get loaded in those tags [e.g., personfactoid_info.html]:

Third: in mytemplates/admin/ modify change_form.html (if you don't have it just copy it from the django-admin app). You must add a placeholder for the new templatetags (probably you want to add it at the bottom of the page):

FOurth: create a new change_form.html in the same directory as above, but under your model template [e.g., mytemplates/admin/myapp/mymodel/change_form.html] so to override the behaviour just for that. The 'object_id' variable is passed by the admin template by default:


Copy this code and paste it in your HTML
  1. # FIRST:
  2.  
  3. from django import template
  4. from poms.pomsapp import models
  5.  
  6. register = template.Library()
  7.  
  8. # for People template
  9.  
  10. @register.inclusion_tag('admin/snippets/personfactoid_info.html')
  11. def display_personfactoids(person_id):
  12. person = models.Person.objects.get(id__exact=person_id)
  13. # factoids = models.Factoids.objects.filter(people=person)
  14. return { 'person': person }
  15.  
  16.  
  17. ## SECOND:
  18.  
  19. {% if person.assocfactoidperson_set.all %}
  20.  
  21. <b>Person associated to factoids:</b></br />
  22.  
  23. <table>
  24. <tr>
  25. <th>Record ID</th>
  26. <th>Type</th>
  27. <th>Short Summary</th>
  28. <th>Role</th>
  29. <th>Source</th>
  30. </tr>
  31.  
  32. {% for a in person.assocfactoidperson_set.all %}
  33. <tr>
  34. <td>{{a.factoid.id}}</td>
  35. <td>{{a.factoid.get_right_subclass.0}}&#160;&#160;&#160;&#160;</td>
  36. <td><a href="{% url factoid_detail a.factoid.id %}" title="click to show">{{a.factoid}}</a></td>
  37. <td>{{a.role}}</td>
  38. <td><a href="{% url source_detail a.factoid.sourcekey.id %}" title="click to show">{{a.factoid.sourcekey}}</a></td>
  39. </tr>
  40. {% endfor %}
  41.  
  42. </table>
  43.  
  44. <br /><hr><hr><hr><br />
  45. {% endif %}
  46.  
  47.  
  48.  
  49. ## THIRD
  50.  
  51. ........
  52.  
  53. {% block related_items_block %}{% endblock %}
  54.  
  55. ........
  56.  
  57.  
  58. ## FOURTH
  59.  
  60. {% extends "admin/change_form.html" %}
  61. {% load myadmin_tags %}
  62. {% block related_items_block %}
  63. <h2>Additional information:</h2><br />
  64.  
  65. {% if object_id %}
  66. {% display_personfactoids object_id %}
  67. {% endif %}
  68.  
  69. {% endblock %}

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.