簡體   English   中英

如何在模板中獲取 ImageField URL?

[英]How to get an ImageField URL within a template?

我的 Django 應用程序中有以下模型:

class Image(models.Model):
    image = models.ImageField(
        upload_to='images/', 
        height_field='height', 
        width_field='width'
    )
    credit = models.CharField(max_length=50, blank=True)
    caption = models.TextField(blank=True)
    article = models.ForeignKey(Article)
    width = models.PositiveIntegerField(
        blank = True, null = True,
        editable = False,
        default = 0
    )
    height = models.PositiveIntegerField(
        blank = True, null = True,
        editable = False,
        default = 0
    )

我已將 MEDIA_ROOT 設置為 Apache Web 根目錄中名為 /hmedia/ 的目錄,並將 MEDIA_URL 設置為'http://localhost/hmedia/' 這似乎奏效了——我已經通過 Django 管理站點成功上傳了幾張圖片,我可以通過http://localhost/hmedia/images/[filename]查看這些圖片。 Django 管理站點實際上向我顯示了文件名和指向每個圖像的實時 URL 的鏈接,並且這些鏈接有效。

我的問題是,我不知道如何在我的模板中獲取這些圖像的 URL 或文件名:

<ul>
{% for image in article.image_set.all %}
    <li>Caption: "{{image.caption}}", Credit: "{{image.credit}}", URL: "{{image.url}}"</li>
{% endfor %}
</ul>

上面的模板導致這個輸出:

<ul>

    <li>Caption: "here's an image caption", Credit: "some photographer", URL: ""</li>

    <li>Caption: "Another caption here", Credit: "Another photographer", URL: ""</li>

</ul>

換句話說,它正確地輸出了每個圖像的標題和信用屬性,但它沒有為 {{image.url}} 輸出任何內容。

如何在我的模板中獲取圖像的 URL? Django 管理界面模板是如何做到的? (我已經在管理模板目錄中扎根,但找不到我要找的東西。)

你需要的是{{ image.image.url }} & {{ image.image.path }} ,而{{ image }} - 只是一個 Image 對象,定義模型的實例和{{ image.image }}得到我們到作為ImageField對象並提供所有指定屬性的字段。

將您的代碼更改為:

<ul>
{% for image in article.image_set.all %}
    <li>Caption: "{{ image.caption }}", Credit: "{{ image.credit }}", URL: "<a href ='/{{ image.image }}'{{ image.image }}</a>"</li>
{% endfor %}
</ul>

將 {{ image.url }} 更改為 {{ image.image }} 因為 '.image' 包含圖像的位置。 您沒有從 '.url' 獲得任何值的原因是您的模型中的 Image 類中沒有字段 url。

如果您在 forms.py 中有 ImageForm 並將 Form Instance 傳遞給您的 django 模板,那么 src="{% get_media_prefix %}{{your-form-instance.instance.image}}" 將解決您的問題。

模型.py

class Addon(models.Model):
    image = models.FileField(upload_to='addons/', null=True)
    addon_name = models.CharField(max_length=100, null=False, blank=False)

表格.py

class AddonForm(forms.ModelForm):
    addon_name = forms.CharField(max_length=100, required=True)
    image = forms.ImageField(required=False)

視圖.py

@require_http_methods(['GET'])
def addons(request, ):
    addon_form_set = modelformset_factory(Addon, form=AddonForm, extra=1)
    addon_forms = addon_form_set(queryset=Addon.objects.all())
    return render(request, 'addons/addons.html', {'form': addon_forms)

your_templates.html

{% for data in form %}
   <img src="{% get_media_prefix %}{{data.instance.image}}"/>
{% endfor %}

在您的 django 項目中創建一個文件夾“static”並將其添加到您的設置文件中

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

如果你的設置文件中沒有這一行,也添加它

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

現在在模板中您可以使用

{% static  image.image.url %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM