Django ImageField 不会显示在网页上 [英] Django ImageField will not display on webpage

查看:21
本文介绍了Django ImageField 不会显示在网页上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Django 模型中使用了一个 imageField,当我尝试在 html 中显示它时,照片没有被识别.

I am using an imageField in my Django model and the photo is not being recognized when I try to display it in the html.

这是我的models.py

Here is my models.py

from django.db import models

class LatestRelease(models.Model):
    title = models.CharField(max_length=50)
    description = models.CharField(max_length=300)
    cover = models.ImageField(upload_to='personal/static/img/')
    display = models.BooleanField(default=True)

这里是view.py

from django.shortcuts import render
from personal.models import LatestRelease

def index(request):
    releases = LatestRelease.objects.all()
    return render(request, "personal/home.html", {'releases': releases})

HTML 代码

 {% for release in releases %}
    <img src="{{ release.cover.url }}" class='' height='235' width='235'>
 {% endfor %}

所以基本上,图片路径 {{ release.cover.url }}/personal/static/img/[image name]当我使用 url 路径 /static/img/[image name] 作为源时,我的照片才会出现.

So basically, the image path {{ release.cover.url }} is /personal/static/img/[image name] and my photos only appear to show up when I use the url path /static/img/[image name] as the source.

无论如何要从 {{ release.cover.url }} 创建的字符串中取出 /personal 吗?或者让我的 img 源接受 /personal/static/img/[image name] 下的文件?

Is there anyway to take /personal out of the string that is created from {{ release.cover.url }} ? Or to make it so that my img source accepts files under /personal/static/img/[image name]?

推荐答案

如果你也展示你的 settings.py 和 urls.py 文件可能会有所帮助,但我猜你的未设置开发服务器来提供该 personal/static/img/[image name] 文件.要进行设置,您需要调整 settings.py 和主项目 urls.py 文件.

It might help if you showed your settings.py and urls.py files too but I'm guessing that your development server is not set up to serve that personal/static/img/[image name] file. To set that up you need to adjust your settings.py and main project urls.py files.

settings.py:

# Add these
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"

MEDIA_ROOT 指示当您的 MEDIA_URL 被点击时(例如当您的浏览器要求该图片时)可以从何处提供文件.为了实际提供设置视图所需的文件,有一个内置的帮助函数用于在开发中提供这些文件,将其添加到您的主项目 urls.py 文件中

MEDIA_ROOT indicates where files can be served from when your MEDIA_URL is hit (like when your browser asks for that picture). To actually serve the files you need to set up a view, there is a built-in helper function for serving this files in development, add this to your main project urls.py file

urls.py:

from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    # your other views
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

然后你应该再次上传图片,它会保存在media//,在你的情况下media/personal/static/img/<图片名称>.你的服务器实际上会知道现在在哪里可以找到它.

Then you should just upload the picture again, it will be saved in media/<upload_to>/<picturename>, in your case media/personal/static/img/<picture_name>. And your server will actually know where to find it now.

检查文档 一个更好的例子.

Check out the documentation for a little better example.

您使用的硬编码 url /static/img/[image name] 有效,因为 Django 开发服务器默认可以从 static/<项目根目录中的/code> 目录以及应用程序根目录中的任何 static/ 文件夹.因此,您在 ImageField 中的 upload_to 争论创建了那个 /personal/static/ 文件夹,这是一种幸运的巧合.然后因为 DEBUG = True 在 settings.py 中设置,你被设置为自动提供它.这在上面的链接文档中有描述(更好).

The hard-coded url that you used, /static/img/[image name] worked because the Django development server can, by default, serve static files from the static/ directory in the project root as well as any static/ folders in the application root directories. So it was a kind of happy coincidence that your upload_to arguement in your ImageField created that /personal/static/ folder. Then since DEBUG = True was set in settings.py you were setup to serve it automatically. This is described (better) in the linked documentation above.

这篇关于Django ImageField 不会显示在网页上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆