如何在Django Photologu中显示扩展字段

2024-05-15 22:25:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经阅读了有关如何customize models的文档。我想添加一个额外的网址。在

extra_url = models.URLField(max_length=200, blank=True, null=True)

在Django管理中一切都很好,但是我不知道如何在实际的HTML中调用该字段。在

文件上说:

The above code is enough to start entering tags in the admin interface. To use/display them in the front end, you will also need to override Photologue’s own templates - as the templates are likely to be heavily customised for your specific project, an example is not included here.

我已经用我自己的模板覆盖了Photologue的模板。我是个新手,没有例子就搞不懂。我一直在猜测,运气不好。在

^{pr2}$

Tags: thetoin文档模板trueurlis
1条回答
网友
1楼 · 发布于 2024-05-15 22:25:36

您是否完全按照文档创建额外的模型,即:

class GalleryExtended(models.Model):

    # Link back to Photologue's Gallery model.
    gallery = models.OneToOneField(Gallery, related_name='extended')

    # Now your extra field.
    extra_url = models.URLField(max_length=200, blank=True, null=True)

如果是-则可以在模板中编写{{ gallery.extended.extra_url }}来显示新字段。在

关键是related_name,它告诉Django如何从现有的库中访问新模型,并且您已经在模板中以变量{{ gallery }}访问新模型。在

相关问题 更多 >