Django-图片不会显示在模板中。但是出现了图片网址。 Urls.py问题?

时间:2019-02-06 21:06:15

标签: django django-models django-views django-urls django-imagekit

使用Django 2.1.5 + Bootstrap 4.2和来自fontawesome的一些图标

你好, 经过长时间的搜索后,我决定发布该帖子,因为大多数答案都与旧版Django有关。 我是Django的新手,我喜欢测试所有的Function / Class Views和Modelfields。现在,我想测试如何在模板中显示与数据库链接的图像。但不幸的是,图像不希望显示,我只看到图像占位符图标。

我创建了一个应用程序和2个简单的模型,其中包含音乐家(带有图片)和汽车。可以通过{{modelone.person_pic}}

在模板中访问通过admin上传的作品和空白网址。

解决的问题-请在下面查看

aquaman / models.py

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    person_pic = models.ImageField(upload_to='artist/', max_length=255, null=True, blank=True )

def __str__(self):
    return "{} - {}".format(self.first_name[:25], self.last_name[:25])

class Data_Sheet(models.Model):
    car = models.CharField(max_length=30)
    color = models.CharField(max_length=30)

    def __str__(self):
    return "{} - {}".format(self.car[:25], self.color[:25])

views.py

from django.shortcuts import get_object_or_404, render, redirect
from django.utils import timezone
from django.views.generic.list import ListView
from django.views.generic import TemplateView


from .models import Person, Data_Sheet

<…some other views…>

class ArtistView4(TemplateView):

template_name = 'aquaman/artistview4.html'


def get_context_data(self, **kwargs):
    context = super(ArtistView4, self).get_context_data(**kwargs)
    context['modelone'] = Person.objects.all()
    context['modeltwo'] = Data_Sheet.objects.all()
    return context

artistview4.html

{% extends 'base.html' %}

{% load static %}


{% block custom_css %}
<link rel="stylesheet" type="text/css" href="{% static 'css/clear_styles.css' %}">
{% endblock %}


{% block content %}


{% for visual in modelone %}

<div align="center">
<h4 class="">{{ visual.first_name }}</h4>
<h4 class="">{{ visual.last_name }}</h4>
<h4 class="">{{ visual.person_pic }}</h4> <!-- to check if url is displayed --> 
</div>

<img src="{{ visual.person_pic.url }}">

</div>
{% endfor %}

{% endblock %}

urls.py

from django.urls import path

from . import views
from .views import AquaListView, AquaListView2, AquaListView3, ArtistView4

app_name='aquaman'

urlpatterns = [
    path('home/', views.aqua_home, name='aqua_home'),
    path('aqua_listview/', AquaListView.as_view(), name='aqua_listview'),
    path('aqua_listview2/', AquaListView2.as_view(), name='aqua_listview2'),
    path('aqua_listview3/', AquaListView3.as_view(), name='aqua_listview3'),
    path('arstistview4/', ArtistView4.as_view(), name='artistview4'),
]

我的方法

据我了解的文档。我必须将此行添加到urlpattern中的 urls.py ,以便一切都可以直接进行。

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

所以我的 urls.py 现在看起来像这样

from django.urls import path

from django.conf import settings
from django.conf.urls.static import static

from . import views
from .views import AquaListView, AquaListView2, AquaListView3, ArtistView4

app_name='aquaman'

urlpatterns = [
    path('home/', views.aqua_home, name='aqua_home'),
    path('aqua_listview/', AquaListView.as_view(), name='aqua_listview'),
    path('aqua_listview2/', AquaListView2.as_view(), name='aqua_listview2'),
    path('aqua_listview3/', AquaListView3.as_view(), name='aqua_listview3'),
    path('arstistview4/', ArtistView4.as_view(), name='artistview4'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这就是我在 settings.py

中添加的内容

TEMPLATES = [
    {
<…>
 'django.template.context_processors.media',
<…>
    },
]


STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    '/var/www/static/',
]

MEDIA_URL = '/media/'
MEDIA_ROOT =  os.path.join(BASE_DIR, 'media')

到目前为止,没有任何变化。 Django在模板中正确显示了我的姓,但是我只看到图像占位符,没有图像。

Django服务器#

Not Found: /media/artist/freddy_mercury.jpg
Not Found: /media/artist/kurt_cobain.jpg
Not Found: /media/artist/ella_fitzgerald.PNG
Not Found: /media/artist/amy_winehouse.PNG
[07/Feb/2019 18:42:49] "GET /media/artist/ella_fitzgerald.PNG HTTP/1.1" 404 2257

当我通过管理员从桌面上载图片时,Django会自动将图片保存在 MyProject / media / artist> 中,但同时django找不到这些图片?

例如,如果直接通过 http://127.0.0.1:8000/media/artist/kurt_cobain.jpg 转到图像,django会向我显示以下内容:

The current path, media/artist/kurt_cobain.jpg, didn't match any of these.

即使django通过上传将图片放置在 BaseDir / media / artist 文件夹中。

也许我错过了“正在开发中的服务文件”,也就是如果设置。调试: https://docs.djangoproject.com/en/2.1/ref/views/

但是当我添加时,也什么也没有发生。

我真的很感谢所有可能有提示的人

解决的问题

好吧,我误会了文档中的某些内容,也许不太明显。

from django.conf import settings
from django.conf.urls.static import static

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

这必须添加到“ MainApp / urls.py” 中,而不要添加到您的“ MyOtherApps / urls.py” 中。我以为只是因为我只需要特定应用程序中的图像,所以我必须在这些特定应用程序urls.py中添加这些行。

和平与和平

0 个答案:

没有答案