我创建了一个简单的 django 博客,但无法让图像始终显示在我的帖子中。具体来说,我的图像出现在“列表” View (网站的主页)中,但不在“详细信息” View 中(查看单个帖子时)。如果您想实时查看该问题,请访问以下网站:endicott.co
ListView 的代码如下:
{% 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 %}
详细 View 的代码如下:
{% 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 正确显示列表中的图像而不是详细 View 有什么想法吗?让我知道我是否应该发布任何额外的代码以获取更多背景。
文件结构如下:
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)
最佳答案
您的问题是 src=
被理解为相对 URL(就像 saq7
指出的那样)。您希望图像的 src=
指向绝对 URL(以 http://
开头),因为图像位于网站根目录的绝对位置(从域)。
要执行此操作,您应使用{% media post.image.url %}
。请注意,为此您需要执行 couple of additions到 settings.py
,特别是 MEDIA_ROOT
和 MEDIA_URL
,以便 {% media %}
模板标记工作。
以这种方式使用媒体文件(使用 {% media %}
)是最常见和接受的方式。请注意,在生产中(即在真正的网络服务器后面),此类文件不应由 Django 服务器提供,请注意该文章的内容:
This is not suitable for production use!
然而,Django 文档中还有另一篇文章 explaining what to do to configure your webserver to serve these files .
此外,如果您设置 MEDIA_URL = '/media/'
,您将需要从 post.image.url
中删除 media/
前缀>.
关于python - Django 博客中的图像失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37493102/