Python 显示Django模型的图像
我在完成教程后创建了我的第一个网站,我尝试使用模板语言添加一些图像,但是我只得到了一些蓝色问号图标 models.pyPython 显示Django模型的图像,python,django,Python,Django,我在完成教程后创建了我的第一个网站,我尝试使用模板语言添加一些图像,但是我只得到了一些蓝色问号图标 models.py from django.db import models class Galery(models.Model): title = models.CharField(max_length = 120) def __unicode__(self): return self.title class Image(models.Model):
from django.db import models
class Galery(models.Model):
title = models.CharField(max_length = 120)
def __unicode__(self):
return self.title
class Image(models.Model):
galery = models.ForeignKey(Galery)
name = models.CharField(max_length = 50, unique = True)
picture = models.ImageField(upload_to='media')
description = models.CharField(max_length = 500)
materials = models.CharField(max_length = 150)
def __unicode__(self):
return self.name
galery.html
{%extends 'home/base.html'%}
{%load static%}
{%block body%}
<div class="col-lg-12">
<h1 class="page-header">My Projects</h1>
</div>
{%if image%}
{%for n in image%}
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="">
<img class="img-responsive" src="{% static 'media/{{n.image.url}}' %}">
</a>
</div>
{%endfor%}
{%endif%}
{%endblock%}
编辑:忘了提到我从管理员上传了一些图片
编辑2:在我的URL.py中,我有
if settings.DEBUG:
urlpatterns += patterns(
'django.views.static',
(r'media/(?P<path>.*)',
'serve',
{'document_root': settings.MEDIA_ROOT}), )
if settings.DEBUG:
urlpatterns+=模式(
“django.views.static”,
(r'media/(?P.*)),
"发球",,
{'document\u root':settings.MEDIA\u root}),)
默认情况下,Django不为用户上传的图像提供服务。通过添加以下URL配置进行设置:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这只是为了发展。在生产环境中,您将需要以一种更具可扩展性的方式(直接从HTTP服务器或更好的方式,从文件存储服务(如S3或CDN)为您的文件提供服务)
有关详细信息:我将此部分添加到projects url.py中,但当我收到错误“TypeError:url()至少接受2个参数(2个给定参数)”时
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)