qraq
qraq

Reputation: 318

Custom django-admin templates

I've got a model, and my instance called "show_user_image":

class user_image(models.Model):
  title = models.CharField(max_length=50)
  img = models.imageField(upload_to='/home/blabla')

  def show_user_image(self):
    return u'<img src="%s" />' % self.img.url
  show_user_image.short_description = 'User image'
  image_img.allow_tags = True

off course i can use it at my admin list:

list_display = ('title', 'show_user_image')

And my question is: how to use this instance in the edit form?

Something like here: http://new-media.djangobook.com/content/en/1.0/chapter17/book_extra.png

{% extends "admin/change_form.html" %}

{% block form_top %}
  <p>Insert meaningful help message here...</p>
{% endblock %}

but i want:

{% extends "admin/change_form.html" %}

{% block form_top %}
  {{ MY-INSTANCE-HERE }}
{% endblock %}

I just need to image display above the form.

Thanks! John.

Upvotes: 2

Views: 1993

Answers (1)

Mariusz Jamro
Mariusz Jamro

Reputation: 31683

The form is admin template is available via adminform.form variable. Your field is named img, so it will be like this (untested):

{% block form_top %}
   <img src="{{ adminform.form.img.value }}"/>
{% endblock %}

BTW. Class names in python should use CapitalizedWordsNamingConvention, according to official style guide. You should name the model UserImage instead of user_image.

BTW2: show_user_image is a method, not an instance.

Upvotes: 1

Related Questions