【问题标题】:How to fix "Not Found" problem for displaying images?如何解决显示图像的“未找到”问题?
【发布时间】:2019-04-14 20:31:00
【问题描述】:

我正在准备网站的博客组件,它是在项目的应用程序中创建的。好像除了图片不显示没有问题。

运行时,一切正常,但只显示图像“alt”而不是图像。我在这个项目的其他页面中有没有问题的图像。

终端上有报告: “未找到:/article/static/images/growth_EWAl68g.png [2019 年 4 月 14 日 17:44:38] “GET /article/static/images/growth_EWAl68g.png HTTP/1.1”404 6164” 如您所见,Django 使用虚假地址(静态文件夹位于项目根目录)来访问图像。

class BlogArticle(models.Model):
    title = models.CharField(max_length=150)
    headline = models.CharField(max_length=300)
    body = models.TextField()
    post_image = models.ImageField(upload_to ="static/images")
    date = models.DateTimeField(auto_now_add=True)
    author = models.CharField(max_length=100)

    def __str__(self):
        return self.title
class ArticleListView(ListView):
    model = BlogArticle
    template_name = 'article_list.html'
urlpatterns = [
    path('', views.HomeView, name= 'home'),
    path('article/', views.ArticleListView.as_view(), name='article_list'),
    path('article/<int:pk>/', views.ArticleDetailView.as_view(), name='article_detail'),
    .
    .
    .]
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('allauth.urls')),
    path('contactus', include('sendemail.urls')),
    path('', include('myapp.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
{% block content %}

    <div <div class="container mt-5 bg-light">
  {% for article in object_list %}
   <div class="form-row">
    <div class="form-group col-sm-9 ">
        <div class="post-entry">
            <h5 > <a href = "{% url 'article_detail' article.pk %}" > {{ article.title }}</a></h5>
            <div>
              <p class="font-weight-bold"> {{article.headline }} </p>
            </div>         
            <div>
                <span class="text-muted">by {{article.author }} | {{  article.date|date:"M d, Y"}}</span>
            </div>
        </div>      
   </div>
    <div class="form-group col-sm-3 ">
      <div class="thumbnail">
        <img src="{{article.post_image}}" width=100 height=100 class="rounded" alt="{{article.title}}">
      </div>
    </div>
   </div>
  {% endfor %}
  </div>

{% endblock content %}
.
DEBUG = True
ALLOWED_HOSTS = []
.
. 
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
.
.

这个条件我看不懂,看来django应该去static/images/但是地址开头多了一个词(文章)。

【问题讨论】:

  • 如果我删除 url.py 中的主页路径并将博客 url 更改为“path('', views.ArticleListView.as_view(), name='article_list')”,问题就解决了,但我不希望博客页面作为网站主页。

标签: django imagefield detailview


【解决方案1】:

这个问题简单的添加就解决了

urlpatterns +=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

到项目的 urls.py 和

MEDIA_URL = '/媒体/' 在 settings.py 中。

【讨论】:

    猜你喜欢
    • 2017-05-10
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2017-11-03
    • 2021-03-31
    • 1970-01-01
    相关资源
    最近更新 更多