Django ImageField链接到错误的目录

时间:2014-07-24 23:01:36

标签: django python-3.x django-admin

我的Django管理页面将图像保存到正确的目录(... / project / media / article /),但当我使用 Article.image.url 在模板中引用它时url然后指向http:// localhost /article/image.jpg而不是http:// 的本地主机 /media/article/cat.jpg

我花了大概4个小时试图解决这个问题但没有用。我究竟做错了什么?该模板完美呈现图像所在的默认“图像缺失”/“断开链接”图标。

提前致谢

这是我的poject urls文件:

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 %}

1 个答案:

答案 0 :(得分:1)

尝试更改:

MEDIA_URL = '/'

为:

MEDIA_URL = '/media/'

并尝试改变:

 <img src='{{ article.image.url }}' 

为:

 <img src="{{ MEDIA_URL }}{{ article.image }}" >