gpt4 book ai didi

python - 无法在数据库的 html 页面上显示图像

转载 作者:太空宇宙 更新时间:2023-11-04 05:55:00 25 4
gpt4 key购买 nike

我只看到带有/media/image.jpg 等 url 的图标。我认为 urls.py 有问题,但我不知道如何解决。有人可以帮助我吗?

设置.py:

...
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
....

模型.py:

....
class Image(models.Model):
product = models.OneToOneField(Product)
top_image = models.ImageField(upload_to='media/', verbose_name ="Главное изображение")
second_image = models.ImageField(blank = True, verbose_name = "Изображение 2")
third_image = models.ImageField(blank = True, verbose_name = "Изображение 3")
fourth_image = models.ImageField(blank = True, verbose_name = "Изображение 4")
fifth_image = models.ImageField(blank = True, verbose_name = "Изображение 5")

def __unicode__(self):
return u'%s' % self.product

class Meta():
db_table = "Image"
...

文章.html

...
{% for image in product_images %}
<div>
<img src="{{ image.top_image.url }}">
</div>
{% endfor %}
...

网址.py

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mypoject.views.home', name='home'),
url(r'^', 'article.views.articles'),
)

最佳答案

我认为你必须在你的 urls.py 文件中添加这个:

urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

小心你必须只将它用于开发:Django doc

关于python - 无法在数据库的 html 页面上显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28272974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com