Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:如何在模板中显示数据库中的图像?_Python_Django_Image_Templates_Django Templates - Fatal编程技术网

Python Django:如何在模板中显示数据库中的图像?

Python Django:如何在模板中显示数据库中的图像?,python,django,image,templates,django-templates,Python,Django,Image,Templates,Django Templates,嗨,我不知道为什么,但我无法显示数据库中的图像。我想这是因为我在模板中的路径 我的模型: class Article(models.Model): titre = models.CharField(max_length=200) auteur = models.CharField(max_length=200) pub_date = models.DateTimeField('date de publication') numero = models.CharFi

嗨,我不知道为什么,但我无法显示数据库中的图像。我想这是因为我在模板中的路径

我的模型:

class Article(models.Model):
    titre = models.CharField(max_length=200)
    auteur = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date de publication')
    numero = models.CharField(max_length=200)
    categorie = models.CharField(max_length=200, default="culture")
    contenu = models.CharField(max_length=200000)
    image = models.ImageField(upload_to='image_publie', blank=True, null=True)

    def __str__(self):
        return self.titre

    def was_published_recently(self):
        return self.pub_date >= timezone.now() -datetime.timedelta(days=7)
我的看法是:

def detail(request, article_id):
    article = get_object_or_404(Article, pk=article_id)
    return render(request, 'gazette/detail.html', {'article':article})
我的网址:

path('/',views.detail,name='detail'),

设置:

STATIC_URL = '/static/'
MEDIA_URL = '/images/'
MEDIA_ROOT = os_path.join(BASE_DIR, 'images/')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
我的模板:

{% extends "base.html" %}
{% load static %}

{% block content %}
<h1>{{ article.contenu }}</h1>
<ul>
 {% if article.image %}

    <img src="{{article.image.url}}" alt="{{article.titre}}" style="width: 200px; height: 200px;">

 {% endif %}
</ul>
{% endblock %}
{%extends“base.html”%}
{%load static%}
{%block content%}
{{article.contenu}
    {%if article.image%} {%endif%}
{%endblock%}
以及:

以及我在模板上看到的内容

urlpatterns

from django.conf import settings

if settings.DEBUG:
    from django.conf.urls.static import static
    urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

你能详细说明“无法加载”是什么意思吗?另外,在加载后向我们显示生成的HTML,如下所示:
from django.url导入路径,include from。从django.conf导入视图从django.conf.URL导入设置从django.contrib.staticfiles.URL导入静态文件\u urlpatterns urlpatterns=[path('/',views.detail,name='detail'),]urlpatterns+=staticfiles\u urlpatterns()urlperns+=static(settings.MEDIA\u URL,document\u root=settings.MEDIA\u root)if settings.DEBUG:from django.conf.urls.static导入静态urlpatterns+=static(settings.MEDIA\u URL,document\u root=settings.MEDIA\u root)