Reputation: 35
The error message is:
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']
I have a Project MODEL, that takes an image. I added the model to my admin page, and I created an object from admin and can add an image no problem. The problem happens when I try to display my views. I am not even making a reference to the image in base.html.
My Project model (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
My settings.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>
The bottom lines in the Traceback:
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="/~!*()'")
My views.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)
Template for 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 %}
Upvotes: 3
Views: 6747
Reputation: 32244
You should not be passing the image to the static tag
src="{% static project.image %}"
You should use the url
method to get a URL for the image
src="{{ project.image.url }}"
Even though you have commented out the element in your template, because you have not used a Django template comment, the tag is still being rendered
Upvotes: 7