覆盖 Django 管理模板时如何访问模型数据?

发布于 2024-09-07 05:52:03 字数 1606 浏览 2 评论 0原文

抱歉,如果这是一个显而易见的问题,但我已经搜索了几天,但未能得出结果。

我正在创建一个简单的照片库应用程序。有四个画廊,每个画廊包含一张照片(照片由“之前”图像、“之后”图像和标题组成)。我正在尝试使用 django-admin 允许用户单击图库,然后添加照片。

我正在使用 TabularInline 编辑每个图库中的照片。除了 TabularInline 上的默认列之外,我还想添加一个列,显示“之前”照片和“之后”照片的缩略图预览(我为此使用简单缩略图)。经过大量搜索后,最好的方法似乎是覆盖 django-admin tabularInline.html 模板并自己添加列 - 所以我创建了另一个副本并现在尝试编辑它。

我想做的只是引用我要覆盖的 Django 管理模板中的 Photo 对象 - 但我不知道要使用适当的标签。我需要引用,以便我可以使用它与 easy-thumbnails 缩略图标签结合使用......但对于我来说,我无法找出引用该对象的模板标签。我尝试过迭代 ModelForm、FormSet 和 FieldSet 对象,但似乎没有一个对象给我直接引用该对象。

# models.py

class Gallery(models.Model):
    name = models.CharField(max_length=200)
    url = models.CharField(max_length=200)
    desc = models.TextField()

    def __unicode__(self):
    return self.name

class Photo(models.Model):
    gallery = models.ForeignKey(Gallery)
    before = models.ImageField(upload_to='gallery')
    after = models.ImageField(upload_to='gallery')
    caption = models.CharField(max_length=1000)
    order = models.IntegerField(blank = True, null = True)

def __unicode__(self):
        return "Photo " + str(self.order)    

# admin.py
class GalleryForm(forms.ModelForm):
model = Gallery
class Media:
    js = (
        '/assets/js/jquery-1.4.2.min.js',
        '/assets/js/jquery-ui-1.8.2.custom.min-admin-sortable.js',
        '/assets/js/menu-sort.js',
    )        

class PhotoInline(admin.TabularInline):
    model = Photo
    extra = 1
    template = "admin/tabular-thumbnails.html"

admin.site.register(Gallery, 
inlines=[PhotoInline],
form = GalleryForm)

非常感谢,如果我可以提供任何其他信息,请告诉我。我正在使用 Django 1.1

sorry if this is an obvious question but I have been searching for a few days and have not been able to come up with a result.

I am creating a simple photo gallery app. There are four galleries, each containing a photo (the photo consists of a 'before' image, 'after' image and caption). I am trying to use django-admin to allow users to click on a gallery and then add photos.

I am using a TabularInline to edit the photos within each gallery. In addition to the default columns on the TabularInline, I would like to add a column that shows a thumbnail preview of the 'before' photo and 'after' photo (I am using easy-thumbnails for this). After much searching, it seems like the best way to do this is to override the django-admin tabularInline.html template and add the column myself - so I created another copy and am trying to edit it now.

What I would like to do is simply reference the Photo object within the Django admin template that I am overriding - but I don't know the appropriate tag to use. I need the reference so I can use it in conjunction with the easy-thumbnails thumbnail tag ... but for the life of me I cannot figure out the template tag that references the object. I have tried iterating through the ModelForm, FormSet, and FieldSet objects but none seem to give me a direct reference to the object.

# models.py

class Gallery(models.Model):
    name = models.CharField(max_length=200)
    url = models.CharField(max_length=200)
    desc = models.TextField()

    def __unicode__(self):
    return self.name

class Photo(models.Model):
    gallery = models.ForeignKey(Gallery)
    before = models.ImageField(upload_to='gallery')
    after = models.ImageField(upload_to='gallery')
    caption = models.CharField(max_length=1000)
    order = models.IntegerField(blank = True, null = True)

def __unicode__(self):
        return "Photo " + str(self.order)    

# admin.py
class GalleryForm(forms.ModelForm):
model = Gallery
class Media:
    js = (
        '/assets/js/jquery-1.4.2.min.js',
        '/assets/js/jquery-ui-1.8.2.custom.min-admin-sortable.js',
        '/assets/js/menu-sort.js',
    )        

class PhotoInline(admin.TabularInline):
    model = Photo
    extra = 1
    template = "admin/tabular-thumbnails.html"

admin.site.register(Gallery, 
inlines=[PhotoInline],
form = GalleryForm)

Thanks so much in advance and please let me know if there's any additional information I can offer. I am using Django 1.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滿滿的愛 2024-09-14 05:52:21

{{ form.instance }} 将始终是与模型表单关联的模型实例(假设有一个)。

(请注意,“{{ formset.instance }}”是内联表单集中父模型的实例)。

{{ form.instance }} will always be the model instance associated with a modelform, assuming there is one.

(Note that ``{{ formset.instance }}` is the instance of the parent model in an inline formset).

岁月无声 2024-09-14 05:52:17

在 Django 2.1 {{adminform.form.instance.field_name}} 中为我工作。

In Django 2.1 {{adminform.form.instance.field_name}} worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文