在django中显示来自管理面板的图像时出错

时间:2017-07-28 06:48:47

标签: python django recommendation-engine

我正在尝试从django管理面板上传图片,我做了以下更改。但是,当我尝试在wine_list页面和review_list中加载图像时出现此错误。图像存储在媒体中但是,它不是正在显示:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/static/media/download_1.jpg
Raised by:  django.views.static.serve
'media\download_1.jpg' could not be found

这是wine_list

的代码
{% extends 'base.html' %}

{% block title %}
<h2>Wine list</h2>
{% endblock %}

{% block content %}
{% if wine_list %}
{% load static %}

<div>
    {% for wine in wine_list %}
     <div><ul>
    <li>
        <h4><a href="{% url 'reviews:wine_detail' wine.id %}">
        {{ wine.name}}</a>
         <br>
        <a><img src="{% static wine.images.url %}" height="200"></a>  
        <br>
        <br>

这是review_list中图片部分的代码:

{% extends 'base.html' %}

{% block title %}
<h2>Latest reviews</h2>
{% load static %}
{% endblock %}

{% block content %}
{% if latest_review_list %}
<div class="row">
    {% for review in latest_review_list %}
    <div class="col-xs-6 col-lg-4">




        <h4><a href="{% url 'reviews:review_detail' review.id %}">
        {{ review.wine.name }}

        </a></h4>
 <br>
       <a><img src="{% static review.wine.images.url %}" height="200"></a>    

winelsma文件夹的urls.py

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

urlpatterns = [
    url(r'^reviews/', include('reviews.urls', namespace="reviews")),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts/', include('registration.backends.simple.urls')),
    url(r'^accounts/', include('django.contrib.auth.urls', namespace="auth")),

]

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

2 个答案:

答案 0 :(得分:1)

检查您的设置文件是否设置了静态图像路径:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

同时检查图像文件权限。它可以从任何地方访问。

答案 1 :(得分:1)

指定于:{{review.wine.images.url}}

<a><img src="{{ review.wine.images.url }}" height="200"></a>