【问题标题】:Image Failure in Django BlogDjango 博客中的图像失败
【发布时间】:2016-05-27 22:23:59
【问题描述】:

我创建了一个简单的 django 博客,但我无法让图像始终显示在我的帖子中。具体来说,我的图像出现在“列表”视图(网站的主页)中,但不在“详细”视图中(查看单个帖子时)。如果您想实时查看该问题,请访问该网站:endicott.co

列表视图的代码如下:

{% extends "blog/base.html" %}

{% block title %}endicott.co{% endblock %}

{% block content %}
<div class="page-header">
    <h1>endicott.co</h1>
    <p>Perspectives on society, data, and economics</p>
</div>

    {% for post in posts %}
        <h2>
            <a href="{{ post.get_absolute_url }}">
                {{ post.title }}
            </a>
        </h2>
        <p class="date">
            Published {{ post.publish }} by {{ post.author }}
        </p>
        <img src="{{ post.image.url }}" alt="img">
        {{ post.body|linebreaks }}
    {% endfor %}
    {% include "pagination.html" with page=page_obj %}
{% endblock %}

详细视图的代码如下:

{% extends "blog/base.html" %}

{% block title %}{{ post.title }}{% endblock %}

{% block content %}
    <h1>{{ post.title }}</h1>
    <img src="{{ post.image.url }}" alt="img">
    <p class="date">
        Published {{ post.publish }} by {{ post.author }}
    </p>
    {{ post.body|linebreaks }}
{% endblock %}

关于为什么 django 正确显示列表中的图像而不是详细视图的任何想法?让我知道我是否应该发布任何其他代码以获取更多背景信息。

文件结构如下:

blog/
-__pycache__/
-migrations/
-templates/
--blog/
---post/
----detail.html
----list.html
---base.html
--pagination.html
-__init__.py
-admin.py
-apps.py
-models.py
-tests.py
-urls.py
-view.py
media/
-img/
--[where images are stored]
mysite/
-__pycache__/
-static/
-__init__.py
-settings.py
-urls.py
-wsgi.py

我的博客 urls.py 文件如下:

from django.conf.urls import url
from . import views



from django.conf import settings
from django.conf.urls.static import static



urlpatterns = [
    # post views
    #url(r'^$', views.post_list, name='post_list'),
    url(r'^$', views.PostListView.as_view(), name='post_list'),
    url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/'\
        r'(?P<post>[-\w]+)/$',
        views.post_detail,
        name='post_detail'),
]




if settings.DEBUG:
    urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

mysite urls.py 文件如下:

from django.conf.urls import include, url
from django.contrib import admin

##### adds
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'', include('blog.urls',
                          namespace='blog',
                          app_name='blog')),
]



if settings.DEBUG:
    urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

【问题讨论】:

标签: python html django


【解决方案1】:

您的问题是 src= 被理解为相对 URL(就像 saq7 指出的那样)。您希望图像的 src= 指向绝对 URL(以 http:// 开头),因为图像位于网站根目录(来自域)的绝对位置。

为此,您应使用{% media post.image.url %}。请注意,为此您需要执行couple of additionssettings.py,尤其是MEDIA_ROOTMEDIA_URL,才能使{% media %} 模板标签起作用。

以这种方式使用媒体文件({% media %})是最常见和被接受的方式。请注意,在生产中(即在真正的网络服务器后面)此类文件不应由 Django 提供服务器,请注意该文章的内容:

这不适合生产使用!

然而,Django 文档中还有另一篇文章explaining what to do to configure your webserver to serve these files


此外,如果您设置了MEDIA_URL = '/media/',则需要从post.image.url 中删除media/ 前缀。

【讨论】:

  • 谢谢,我能够使用这种通用方法修复它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 1970-01-01
相关资源
最近更新 更多