Django 2.0 抛出 AttributeError: ''Image' 对象没有属性 'replace'"

Ari*_*sky 4 django django-templates django-models django-views django-staticfiles

在Django 2.0中尝试将html图像设置为我的静态路径+图像路径时,django开发服务器显示以下错误:

Error during template rendering

In template /Users/arikanevsky/webappgit/ari/templates/ari/software.html, error at line 5

'Image' object has no attribute 'replace'
Run Code Online (Sandbox Code Playgroud)

models.py 中的 Image 类:

class Image(models.Model):
    """This class contains a reference to the ImageField.

    It is part of a base of models comprising the webapp.

    """
    uplpath = '%Y/%m/%d'
    dfltpath = 'page_images/Y/M/D/no_img.jpg'

    image = models.ImageField(upload_to=uplpath, default=dfltpath)

    def __str__(self):
        """Return human readable string of self.image."""
        return self.image.name
Run Code Online (Sandbox Code Playgroud)

来自 urls.py 的 urlpatterns:

urlpatterns = [
    path('', views.index, name='index'),
    path('<int:page_id>/bio/', views.bio, name='bio'),
    path('<int:page_id>/software/', views.software, name='software'),
    path('<int:page_id>/publications/',
     views.publications, name='publications'),
    path('<int:page_id>/career/', views.career, name='career'),
    path('<int:page_id>/education/', views.education, name='education'),
    ]
Run Code Online (Sandbox Code Playgroud)

views.py 中的 index 方法:

def index(request):
    page_list = get_list_or_404(Page.objects.all())
    return render(request, 'ari/index.html', {'page_list': page_list})
Run Code Online (Sandbox Code Playgroud)

index.html 文件(用作命名):

Error during template rendering

In template /Users/arikanevsky/webappgit/ari/templates/ari/software.html, error at line 5

'Image' object has no attribute 'replace'
Run Code Online (Sandbox Code Playgroud)

来自views.py的软件方法:

def software(request, page_id):
    software_page = get_object_or_404(Page, pk=page_id)
    return render(request, 'ari/software.html', {'software_page': software_page})
Run Code Online (Sandbox Code Playgroud)

software.html 文件:

class Image(models.Model):
    """This class contains a reference to the ImageField.

    It is part of a base of models comprising the webapp.

    """
    uplpath = '%Y/%m/%d'
    dfltpath = 'page_images/Y/M/D/no_img.jpg'

    image = models.ImageField(upload_to=uplpath, default=dfltpath)

    def __str__(self):
        """Return human readable string of self.image."""
        return self.image.name
Run Code Online (Sandbox Code Playgroud)

应用程序目录结构如下

(我相信我们可以忽略 .ipynb 和 pycache 目录)

|-akanev
|  |-__pycache__
|-ari
|  |-.ipynb_checkpoints
|  |-__pycache__
|  |-migrations
|  |  |-__pycache__
|  |-templates
|  |  |-ari
|  |  |  |-page_images
Run Code Online (Sandbox Code Playgroud)

settings.py 中定义的相关环境目录路径:

MEDIA_ROOT: '/Users/arikanevsky/webappgit/ari/templates/ari/page_images'

MEDIA_URL: ''

BASE_DIR: '/Users/arikanevsky/webappgit'

STATICFILES_DIRS: []

STATIC_ROOT: None

STATIC_URL: '/page_images/'

TEMPLATES:  
[{'APP_DIRS': True,
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'OPTIONS': {'context_processors':   ['django.template.context_processors.debug',
                                   'django.template.context_processors.request',
                                 'django.contrib.auth.context_processors.auth',
                                 'django.contrib.messages.context_processors.messages']}}]
Run Code Online (Sandbox Code Playgroud)

software.html 文件中的 img src 行抛出错误:

Exception Type: AttributeError at /ari/4/software/
Exception Value: 'Image' object has no attribute 'replace'
Run Code Online (Sandbox Code Playgroud)

非常感谢任何和所有线索,因为这个神秘的错误可能是什么!

谢谢 :)

小智 8

您使用的静态模板标记错误。它应该用于您要在页面上使用的静态文件。例如链接到非用户上传的样式表、javascript 或图像。

有关更多信息,请参见此处:https : //docs.djangoproject.com/en/2.0/howto/static-files/

您正在尝试从模型中上传的文件中获取图片 - 可能由用户上传完成(或以非“静态”的方式完成)。这是通过媒体网址和媒体根设置处理的。请参见此处:https : //docs.djangoproject.com/en/2.0/topics/files/

获取图像的正确方法是:

<img src = "{{ software_page.image.url }}" alt = "Profile Picture"/>
Run Code Online (Sandbox Code Playgroud)

此致,

安德烈亚斯