mohi666
mohi666

Reputation: 7042

Displaying HTML fields in Admin Form in Django

I have a charfield in my Django model that stores html elements such as <a href='www.gooogle.com'>Google</a>.

In admin form these fields are shown inside an input of type text. I was wondering how it is possible to show the actual HTML instead of having it inside a text input? For the above example, all I want is a "Google" so that when you click on it, it takes you the address. I don't want admin moderators to see HTML mark-ups.

Note that I have already achieved this in admin list view using list_display. I need to make the same functionality in details form.

Thanks in advance

Upvotes: 0

Views: 1056

Answers (1)

mohi666
mohi666

Reputation: 7042

I found the solution by creating my own widget.

class HtmlWidget(widgets.Widget):
  """A widget to display HTML in admin fields."""

  input_type = None # Subclasses must define this.

  def render(self, name, value, attrs=None):
    if value is None:
      value = ''
    return mark_safe(u'%s' % value)

Upvotes: 1

Related Questions