gpt4 book ai didi

python - Django 错误 "' ImageFieldFile' 对象没有属性 'replace' "

转载 作者:行者123 更新时间:2023-12-04 01:02:38 26 4
gpt4 key购买 nike

错误信息是:

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']

我有一个项目模型,可以拍摄图像。我将模型添加到我的管理页面,并从管理创建了一个对象,并且可以毫无问题地添加图像。当我尝试展示我的观点时,问题就发生了。我什至没有引用 base.html 中的图像。

我的项目模型(models.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

我的设置.py:

STATIC_URL = '/static/'

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

base.html:

<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>

回溯中的底线:

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="/~!*()'")

我的观点.py:

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)

project_index.html 模板:

{% extends "base.html" %}
{% block page_content %}

<h1>Projects</h1>

<div class="row">

{% for project in projects %}

<div class="col-md-4">

<div class="card mb-2">

<!--<img class="card-img-top" src="{% static project.image %}"> {{ project.image.url }} -->
<img class="card-image" src="{{ project.image.url }}">

<div class="card-body">

<h5 class="card-title">{{ project.title }}</h5>

<p class="card-text">{{ project.description }}</p>

<a href="{% url 'project_detail' project.pk %}"

class="btn btn-primary">

Read More

</a>

</div>

</div>

</div>

{% endfor %}

</div>

{% endblock %}

最佳答案

您不应该将图像传递给静态标签

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

你应该使用 url 方法来获取图片的 URL

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

即使你已经注释掉了模板中的元素,因为你没有使用 Django 模板注释,标签仍在渲染中

关于python - Django 错误 "' ImageFieldFile' 对象没有属性 'replace' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57942282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com