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

查看:73
本文介绍了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.

这是我的模特.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/<upload_to>/<picturename>中,对于您的情况是media/personal/static/img/<picture_name>.您的服务器实际上会知道 现在在哪里找到它.

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/目录中的静态文件以及应用程序中任何static/文件夹的静态文件根目录.因此,您在ImageField中的upload_to争论创建了该/personal/static/文件夹是一种快乐的巧合.然后,由于在settings.py中设置了DEBUG = True,因此可以自动提供它.在上面的链接文档中对此进行了描述(更好).

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天全站免登陆