Answer the question
In order to leave comments, you need to log in
How to use an image uploaded through the Django admin?
Hello everyone, I'm blogging on Django. I want to be able to add an image to the post through the admin panel, which will then be displayed.
At the moment, I made a model for the post, which has a field
img = models.FileField( upload_to='blog/static/uploads/' ).
How to correctly specify the src attribute for the template in order for this image to be displayed correctly?
Later in the template I tried 2 options. First:
{% for post in posts %}
...
img src="{{ post.img }}"
...
Result: the image is not displayed. Second:
img src=" {% static post.img %}"
compiles to static/blog/uploads/file_name.jpg, i.e. the full path to the image is added to the staticfiles path, which, of course, I don’t need at all.
Please help me understand, maybe I initially went the wrong way? Or how to use the files added from the admin later?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question