rendering HTML as a select item

44 views
Skip to first unread message

Larry Martell

unread,
Jan 19, 2014, 6:50:43 PM1/19/14
to django...@googlegroups.com
I am trying to 'fake out' a model form and display data from 2 columns
in one field. I can do this by returning the 2 columns in the model's
__unicode__ method, but I want to apply styling to the data - I want
one column's data left justified and the other column's data right
justified. When I return this in the __unicode__ method:

return mark_safe('<div style="float: left">%s</div><div style="float:
right">%s</div>' % (self.name, self.group_by))

The HTML gets stripped out and I end up with just the data, e.g.:

<option value="1">12S target</option>

If I return the markup without the mark_safe, the HTML generated looks
like this:

<option value="1">&lt;div style="float:
left"&gt;12S&lt;/div&gt;&lt;div style="float:
right"&gt;target&lt;/div&gt;</option>

But what is displayed in the select box on the browser is:

<div style="float: left">12S</div><div style="float: right">target</div>

Does anyone know how I can do this?

Bill Freeman

unread,
Jan 21, 2014, 10:53:21 AM1/21/14
to django-users
Have you confirmed that it isn't the browser doing this?

That is, with the mark safe in place, have you looked at the output with curl?

Or have you made a static html page with the desired construct in it, and viewed it in the browser?

It may bee that option elements can't contain block level elements.  You could see what you can do with span instead of div.

Bill



--
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...@googlegroups.com.
To post to this group, send email to django...@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/CACwCsY7uh%2BWruXhaD5h8cxpfzHxGghEpFdxwyAahW%2Ba2qHEfjg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Larry Martell

unread,
Jan 21, 2014, 10:56:42 AM1/21/14
to django...@googlegroups.com
On Tue, Jan 21, 2014 at 8:53 AM, Bill Freeman <ke1...@gmail.com> wrote:
> Have you confirmed that it isn't the browser doing this?
>
> That is, with the mark safe in place, have you looked at the output with
> curl?
>
> Or have you made a static html page with the desired construct in it, and
> viewed it in the browser?
>
> It may bee that option elements can't contain block level elements. You
> could see what you can do with span instead of div.

I discovered that <option> elements only allow "text with eventually
escaped characters (like &eacute;).", not general HTML -
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option.
> https://groups.google.com/d/msgid/django-users/CAB%2BAj0s8mRHEnu5uv%3Dz21ErKeR2t3MaSk%2Bx08Mo%2BKz2yokcO0Q%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages