gpt4 book ai didi

python - Django 博客中的图像失败

转载 作者:行者123 更新时间:2023-11-27 23:27:56 25 4
gpt4 key购买 nike

我已经创建了一个简单的 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 additionssettings.py,特别是 MEDIA_ROOTMEDIA_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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com