OK, I give up, seriously dismayed that Django doesn't have this built in 
already I did as I threatened to and derived a class as follows:


from django.utils import six
from django.utils.safestring import mark_safe
from django.utils.html import conditional_escape
from django.utils.encoding import force_text
from django.forms.models import model_to_dict, fields_for_model

class DetailViewWithHTML(DetailView):
    fields = None
    field_data = None
    def __init__(self, instance):
        self.fields = fields_for_model(self.model)
        self.field_data = model_to_dict(instance)
                       
    # HTML formatters stolen straight form the Django ModelForm class
    def _html_output(self, normal_row, help_text_html):
        "Helper function for outputting HTML. Used by as_table(), as_ul(), 
as_p()."       
        output = []

        for name, field in self.fields.items():
            value = self.field_data[name]

            if field.label:
                label = conditional_escape(force_text(field.label))
            else:
                label = ''
             
            if field.help_text:
                help_text = help_text_html % force_text(field.help_text)
            else:
                help_text = ''
                
            output.append(normal_row % {
                 'label': force_text(label),
                 'value': six.text_type(value),
                 'help_text': help_text,
             })

        return mark_safe('\n'.join(output))

    def as_table(self):
        "Returns this form rendered as HTML <tr>s -- excluding the 
<table></table>."
        return self._html_output(
            normal_row=
'<tr><th>%(label)s</th><td>%(value)s%(help_text)s</td></tr>',
            help_text_html='<br /><span class="helptext">%s</span>')

    def as_ul(self):
        "Returns this form rendered as HTML <li>s -- excluding the 
<ul></ul>."
        return self._html_output(
            normal_row='<li>%(errors)s%(label)s %(value)s%(help_text)s</li>'
,
            help_text_html=' <span class="helptext">%s</span>')

    def as_p(self):
        "Returns this form rendered as HTML <p>s."
        return self._html_output(
            normal_row='<p>%(label)s %(value)s%(help_text)s</p>',
            help_text_html=' <span class="helptext">%s</span>')

It's a quick hack (if only, sheesh it took some time to work out how 
ModelForm does it and reduce it to the simplest of Data Viewers here) and 
hasn't been exhaustively tested or considered, and I remain blown away that 
DetailView doesn't implement these ... fail Django, fail. 

OK before anyone gets all smart and asks "why would you need that anyway", 
I admit it's not a super powerful generic per se for production 
implementations, but sheesh it's nice to produce a quick instance viewer 
using the same paradigm as the edit form and not have to do something 
different.
     

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/914c4e04-3627-4f53-9243-14a20d50c197%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to