Django错误“'ImageFieldFile'对象没有属性'replace'”

时间:2019-09-15 08:13:00

标签: python django imagefield

错误消息是:

@IBAction func btnDismissTap(_ sender: Any) {
    if let pvc = self.presentationController {
        pvc.delegate?.presentationControllerWillDismiss?(pvc)
    }
    self.dismiss(animated: true, completion: nil)
}

我有一个Project Model,它可以拍摄图像。我将模型添加到管理页面,并从管理创建了一个对象,可以毫无问题地添加图像。当我尝试显示我的视图时会发生问题。我什至没有在base.html中引用该图像。

我的项目模型(models.py):

AttributeError at /projects/

'ImageFieldFile' object has no attribute 'replace'

Request Method:     GET
Request URL:    http://localhost:8000/projects/
Django Version:     2.2.5
Exception Type:     AttributeError
Exception Value:    

'ImageFieldFile' object has no attribute 'replace'

Exception Location:     .../js-portfolio/venv/lib/python3.6/site-packages/django/utils/encoding.py in filepath_to_uri, line 252
Python Executable:  .../js-portfolio/venv/bin/python
Python Version:     3.6.8
Python Path:    

['.../js-portfolio',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '.../js-portfolio/venv/lib/python3.6/site-packages']

我的设置。py

from django.db import models

class Project(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()
    technology = models.CharField(max_length=20)
    image = models.ImageField(default='default.jpg', upload_to='img')

def __str__(self):
    return self.title

base.html

STATIC_URL = '/static/'

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

回溯中的底线

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="container">
        <a class="navbar-brand" href="{% url 'project_index' %}">JS Portfolio</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
              <a class="nav-link" href="{% url 'project_index' %}">Home</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Blog</a>
            </li>
          </ul>
        </div>
    </div>

</nav>

<div class="container">
    {% block page_content %}{% endblock %}
</div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>

我的views.py:

return staticfiles_storage.url(path)

     …

▶ Local vars
.../js-portfolio/venv/lib/python3.6/site-packages/django/core/files/storage.py in url
  url = filepath_to_uri(name)

     …

▶ Local vars
.../js-portfolio/venv/lib/python3.6/site-packages/django/utils/encoding.py in filepath_to_uri

return quote(path.replace("\\", "/"), safe="/~!*()'")

project_index.html的模板:

from django.shortcuts import render
from projects.models import Project

def project_index(request):
    projects = Project.objects.all()
    context = {
        'projects': projects
    }
    return render(request, 'project_index.html', context)

def project_detail(request, pk):
    project = Project.objects.get(pk=pk)
    context = {
        'project': project
    }
    return render(request, 'project_detail.html', context)

1 个答案:

答案 0 :(得分:1)

您不应将图片传递到静态标签

src="{% static project.image %}"

您应该使用url方法获取图片的网址

src="{{ project.image.url }}"

即使您已注释掉模板中的元素,但由于未使用Django模板注释,因此标记仍在呈现中