【发布时间】:2014-07-24 23:01:36
【问题描述】:
我的 Django 管理页面正在将图像保存到正确的目录 (.../project/media/article/),但是当我使用 Article.image.url 在模板中引用它时然后 url 指向 http://localhost/article/image.jpg 而不是 http:// 本地主机/media/article/cat.jpg
我可能花了整整 4 个小时试图解决这个问题,但无济于事。我究竟做错了什么?模板完美呈现,只有一个默认的“图像丢失”/“断开的链接”图标,图像应该在哪里。
提前致谢
这是我的项目网址文件:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
...
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': MEDIA_ROOT}),
)
这里是相关的设置信息:
STATIC_ROOT = os.path.join(os.path.dirname(os.path.dirname(__file__)),'app','static')
STATIC_URL = '/app/static/'
MEDIA_ROOT = os.path.join(os.path.dirname(os.path.dirname(__file__)),'media')
MEDIA_URL = '/'
及相关型号信息:
class Article(models.Model):
image = models.ImageField(upload_to = 'article/', blank=True)
image_caption = models.CharField(max_length=100, blank=True)
更新:这里是相关的模板 html:
{% if article %}
<!-- Large/Medium (big desktops) -->
<li class='list-group-item hidden-xs hidden-sm fourColumns'>
<div class='panel'>
<div class='panel-heading'>
{{ article.pub_date }}
</div>
<div class='panel-body'>
By {{ article.writer }}
</div>
</div>
{% if article.pull_quote and not article.image %}
{{ article.article|truncwords:'50'|linebreaks }}
<blockquote>
<p>
{{ article.pull_quote }}
</p>
</blockquote>
{{ article.article|truncfrom:'50'|linebreaks }}
{% elif article.image and article.image_caption and not article.pull_quote %}
{{ article.article|truncwords:'50'|linebreaks }}
<img src='{{ article.image.url }}' class='img-responsive'/>
<em>{{ article.image_caption }}</em><br/><br/>
{{ article.article|truncfrom:'50'|linebreaks}}
{% elif article.image and not article.image_caption and not article.pull_quote %}
{{ article.article|truncwords:'50'|linebreaks}}
<img src='{{ article.image.url }}' class='img-responsive'/>
{{ article.article|truncfrom:50|linebreaks }}
{% elif article.image and article.image_caption and article.pull_quote %}
{{ article.article|truncwords:'50'|linebreaks }}
<blockquote>
<p>
{{ article.pull_quote }}
</p>
</blockquote>
{{ article.article|truncfromto:'50,100'|linebreaks }}
<img src='{{ article.image.url }}' class='img-responsive'/>
<em>{{ article.image_caption }}</em><br/><br/>
{{ article.article|truncfrom:'100'|linebreaks}}
{% elif article.image and article.pull_quote and not article.image_caption %}
{{ article.article|truncwords:'50'|linebreaks }}
<blockquote>
<p>
{{ article.pull_quote }}
</p>
</blockquote>
{{ article.article|truncfromto:'50,100'|linebreaks }}
<img src='{{ article.image.url }}' class='img-responsive'/>
{{ article.article|truncfrom:'100' }}
{% else %}
{{ article.article|linebreaks }}
{% endif %}
</li>
{% endif %}
【问题讨论】:
-
获取该图像的模板 html 代码是什么样的?
-
@AronYsidoro 我用模板更新了问题。我认为唯一真正重要的部分是
标签之一,但我也包含了周围的文本
标签: django python-3.x django-admin