0

So. This is my first Django project. I am using Django Admin template to add/edit/delete content.

I added option to add image to specific section. Here is the model

class Project(models.Model):
    title = models.CharField(max_length = 100)
    description = models.TextField()
    technology = models.CharField(max_length = 20)
    image = models.ImageField(upload_to = "projects/images/")

And it works. It creates images directory inside project directory which can be found in root directory.

When I load image in template I load it with

<img src="/{{project.image.url}}" alt="" class="card-img-top">

Then in HTML it creates element

<img src="/projects/images/106780.jpg" alt="" class="card-img-top">

But inn console it says that it can't find image on

http://127.0.0.1:8000/projects/images/106780.jpg

My directory has this hierarchy

portfolio
|
|__ blog
|__ projects
    |__ __pycache__
    |__ images
    |__ migrations
    |__ templates
    |__ ...
|__ venv
|__ db.sqlite3
|__ manage.py
Mileta Dulovic
  • 1,036
  • 1
  • 14
  • 33
  • How does your url module look like? Django does not automatically serve files from your project directory. – dhke Aug 31 '19 at 12:49
  • if you are talking about urlpatterns here they are ```urlpatterns = [ path("", views.project_index, name="project_index"), path("/", views.project_detail, name="project_detail") ] ``` – Mileta Dulovic Aug 31 '19 at 12:51
  • Take a look at this answer for clarification on this issue. https://stackoverflow.com/a/22776212/9517176 – labennett Aug 31 '19 at 12:51

2 Answers2

1

Here you are using static path for image

<img src="/{{project.image.url}}" alt="" class="card-img-top"> <img src="/projects/images/106780.jpg" alt="" class="card-img-top">

You need to use dynamic path so that server can understand full path of the image.

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

Hope this can solve your problem. For more infor visit official doc Django version 2.2 - media path

  • No it's not a solution. I don't think that path iteself is problem. Image on that path exists and it triest to find it on that path. I think that thing is that django can't serve image from path and I have no idea why – Mileta Dulovic Aug 31 '19 at 13:17
  • Because you haven't done anything to tell tell Django to serve images. – Daniel Roseman Aug 31 '19 at 13:23
0

if you're working with forms, make sure to add "enctype" source

  • Bro, this is 3 years old, and is answered already – Mileta Dulovic Oct 12 '22 at 09:01
  • Many more will come with the same problem after us, Greetings – Anddy Agudelo Oct 12 '22 at 13:24
  • Not with a 3-year-old version of the language/framework. If that's the case then you'll have much bigger problems – Mileta Dulovic Oct 12 '22 at 17:56
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the [help center](https://stackoverflow.com/help/how-to-answer). – MagnusO_O Oct 15 '22 at 17:19