Wednesday, August 12, 2015

Re: Generic forms (ModelForm) provide HTML for a form! Can generic views (DetailView) do same?

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+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@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+unsubscribe@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.

No comments:

Post a Comment