Changes between Version 32 and Version 33 of NewformsHOWTO


Ignore:
Timestamp:
06/04/08 16:51:30 (7 years ago)
Author:
Victor Andrée <victor.andree@…>
Comment:

fixed the "how add column"-question, and added my own method

Legend:

Unmodified
Added
Removed
Modified
  • NewformsHOWTO

    v32 v33  
    143143from django.contrib.admin.templatetags.admin_list import items_for_result, result_headers
    144144from django.template import Library
     145from django.utils.safestring import mark_safe
    145146register = Library()
    146147
     
    153154        rl = list(items_for_result(cl,res))
    154155        for link in additional_links:
    155         rl.append(link['url_template'] % (VAR,)) # Make sure you have enough VARs for any %s's in your extra content.
     156        rl.append(mark_safe(link['url_template'] % (VAR,))) # Make sure you have enough VARs for any %s's in your extra content. Note mark_safe
    156157        yield rl
    157158
     
    181182
    182183    return extended_result_list(cl, additional_links)
    183 report_result_list = register.inclusion_tag("admin/change_list_results.html")(my_model_result_list)
     184my_model_result_list = register.inclusion_tag("admin/change_list_results.html")(my_model_result_list)
    184185}}}
    185186
     
    190191{% load change_list_extras %}
    191192{% block result_list %}{% my_model_result_list cl %}{% endblock %}
     193}}}
     194
     195'''A variation on the above, using Template'''
     196
     197I wanted to add columns to my model but I didn't like the above method very much, mostly because I would have to
     198state variables in the results function, which I wanted to generalize away together with extended_result_list.
     199
     200Instead of using Python string format (i.e. the (VAR,) above), I take advantage of Django's template system.
     201
     202First, move results and extended_result_list away. I created a separate module called admin_extras and put them (rewritten)
     203in {{{ __init__.py }}}:
     204
     205{{{ admin_extras/__init__.py }}}
     206{{{
     207#!python
     208from django.contrib.admin.templatetags import admin_list
     209from django import template
     210from django.utils.safestring import mark_safe
     211
     212def results(cl, col_templates):   
     213    for res in cl.result_list:
     214        rl = list(admin_list.items_for_result(cl, res))
     215        for col in col_templates:
     216            rendered_col = col.render(template.Context({'obj': res}))
     217            rl.append(mark_safe(rendered_col))
     218        yield rl
     219
     220def extended_result_list(cl, additional_cols):
     221    headers = list(admin_list.result_headers(cl))
     222    for h in additional_cols:
     223        headers.append(h)
     224   
     225    # Parse the templates once
     226    col_templates = [template.Template(col['template']) for col in additional_cols]
     227   
     228    return {
     229        'cl': cl,
     230        'result_headers': headers,
     231        'results': list(results(cl, col_templates))
     232    }
     233}}}
     234
     235These functions can now be hooked up to any model using a custom tag for it. I happen to
     236have an Article-model and want to display an extra column with publishing options.
     237
     238{{{ articles/templatetags/change_list_extras.py }}}
     239{{{
     240#!python
     241import admin_extras
     242from django import template
     243from django.contrib.admin.templatetags import admin_list
     244
     245register = template.Library()
     246
     247@register.inclusion_tag("admin/change_list_results.html")
     248def article_result_list(cl):
     249    additional_cols = (
     250        {'text': 'Actions', 'sortable': False, 'template': '<td><a href="{% url publish_view obj.id %}">Publish</a></td>'},
     251    )
     252    return admin_extras.extended_result_list(cl, additional_cols)
     253# article_result_list = register.inclusion_tag("admin/change_list_results.html")(article_result_list)
     254}}}
     255
     256Note that I've changed 'additional_links' to 'additional_cols' and 'url_template' to more generic 'template'. Inside
     257'template', I can use the context variable 'obj' to refer to the current row. If you should be so inclined, you could of course
     258modify article_result_list to send a dictionary of its own to merge with the one in results.
     259
     260I haven't tested using {{{ {% url %} }}} myself, but I see no reason why it wouldn't work!
     261
     262You use this in a custom change_list-template for your model admin:
     263{{{
     264#!python
     265{% extends "admin/change_list.html" %}
     266{% load change_list_extras %}
     267{% block result_list %}{% article_result_list cl %}{% endblock %}
    192268}}}
    193269
Back to Top