Django 2.0 throws AttributeError: ''Image' object has no attribute 'replace'"
Django 2.0 throws AttributeError: ''Image' object has no attribute 'replace'"
在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'
图像 class 来自 models.py:
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
来自 urls.py 的 url 模式:
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'),
]
索引方法来自views.py:
def index(request):
page_list = get_list_or_404(Page.objects.all())
return render(request, 'ari/index.html', {'page_list': page_list})
index.html文件(按命名使用):
{% if page_list %}
<ul>
{% for page in page_list %}
<a href="/ari/{{page.id}}/bio"><p>My bio page</p></a>
<a href="/ari/{{page.id}}/career"><p>My career page</p></a>
<a href="/ari/{{page.id}}/software"><p>My software page</p></a>
<a href="/ari/{{page.id}}/publications"><p>My publication page</p>. </a>
{% endfor %}
</ul>
{% else %}
<p>No pages are available :(</p>
{% endif %}
来自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})
software.html 文件:
{% load static %}
<img src = "{% static software_page.image %}" alt = "Profile Picture"/>
app目录结构如下:
(我相信我们可以忽略 .ipynb 和 pycache 目录)
|-akanev
| |-__pycache__
|-ari
| |-.ipynb_checkpoints
| |-__pycache__
| |-migrations
| | |-__pycache__
| |-templates
| | |-ari
| | | |-page_images
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']}}]
software.html文件中的img src行抛出错误:
Exception Type: AttributeError at /ari/4/software/
Exception Value: 'Image' object has no attribute 'replace'
希望能提供有关此神秘错误可能是什么的所有线索!
谢谢:)
您使用的静态模板标签有误。它应该用于您要在页面上使用的静态文件。例如链接到样式表、javascript 或非用户上传的图像。
查看此处了解更多信息:
https://docs.djangoproject.com/en/2.0/howto/static-files/
您正在尝试从模型中上传的文件中获取图片 -
可能由用户上传完成(或以非 "static" 的方式完成)。这是通过媒体 url 和媒体根设置来处理的。看这里:
https://docs.djangoproject.com/en/2.0/topics/files/
获取图像的正确方法是:
<img src = "{{ software_page.image.url }}" alt = "Profile Picture"/>
此致,
安德烈亚斯
在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'
图像 class 来自 models.py:
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
来自 urls.py 的 url 模式:
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'),
]
索引方法来自views.py:
def index(request):
page_list = get_list_or_404(Page.objects.all())
return render(request, 'ari/index.html', {'page_list': page_list})
index.html文件(按命名使用):
{% if page_list %}
<ul>
{% for page in page_list %}
<a href="/ari/{{page.id}}/bio"><p>My bio page</p></a>
<a href="/ari/{{page.id}}/career"><p>My career page</p></a>
<a href="/ari/{{page.id}}/software"><p>My software page</p></a>
<a href="/ari/{{page.id}}/publications"><p>My publication page</p>. </a>
{% endfor %}
</ul>
{% else %}
<p>No pages are available :(</p>
{% endif %}
来自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})
software.html 文件:
{% load static %}
<img src = "{% static software_page.image %}" alt = "Profile Picture"/>
app目录结构如下:
(我相信我们可以忽略 .ipynb 和 pycache 目录)
|-akanev
| |-__pycache__
|-ari
| |-.ipynb_checkpoints
| |-__pycache__
| |-migrations
| | |-__pycache__
| |-templates
| | |-ari
| | | |-page_images
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']}}]
software.html文件中的img src行抛出错误:
Exception Type: AttributeError at /ari/4/software/
Exception Value: 'Image' object has no attribute 'replace'
希望能提供有关此神秘错误可能是什么的所有线索!
谢谢:)
您使用的静态模板标签有误。它应该用于您要在页面上使用的静态文件。例如链接到样式表、javascript 或非用户上传的图像。
查看此处了解更多信息: https://docs.djangoproject.com/en/2.0/howto/static-files/
您正在尝试从模型中上传的文件中获取图片 - 可能由用户上传完成(或以非 "static" 的方式完成)。这是通过媒体 url 和媒体根设置来处理的。看这里: https://docs.djangoproject.com/en/2.0/topics/files/
获取图像的正确方法是:
<img src = "{{ software_page.image.url }}" alt = "Profile Picture"/>
此致,
安德烈亚斯