On 12/08/2015 8:54 PM, Bernd Wechner wrote:
OK, I give up, seriously dismayed that Django doesn't have this built in
already

Well done Bernd! Welcome to open source. Unfortunately, I can't use your code unless it is part of Django. I have tried implementing my own improvements in the past but I always encounter difficulties when the next release of Django comes.

Could you please get the core developers to adopt your code so I can use it?

Thanks

Mike


I did as I threatened to and derived a class as follows:


|
fromdjango.utils importsix
fromdjango.utils.safestring importmark_safe
fromdjango.utils.html importconditional_escape
fromdjango.utils.encoding importforce_text
fromdjango.forms.models importmodel_to_dict,fields_for_model

classDetailViewWithHTML(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 =[]

       forname,field inself.fields.items():
            value =self.field_data[name]

           iffield.label:
                label =conditional_escape(force_text(field.label))
           else:
                label =''
            Â
           iffield.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,
             })

       returnmark_safe('\n'.join(output))

   defas_table(self):
       "Returns this form rendered as HTML <tr>s -- excluding the
<table></table>."
       returnself._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>')

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

   defas_p(self):
       "Returns this form rendered as HTML <p>s."
       returnself._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
<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to django-users@googlegroups.com
<mailto: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
<https://groups.google.com/d/msgid/django-users/914c4e04-3627-4f53-9243-14a20d50c197%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
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/55CB3D86.7070403%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Reply via email to