Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django images will not load
#4
look!
In your div section the image path you provided in the src attribute of the <img> tag is correct.
But you have to use {% url %} template tag to generate the image URL dynamically For example:-

<div class='p-4 m-4' style="height: 300px; background-color: rgba(255,0,0,0.1);">
<img src="{% url 'image_url_name' 'pic1.jpg' %}" alt="Photo 1">
</div>

* In the above code replace 'image_url_name' with the name of the URL pattern you have defined for serving images in your Django project.

* Now next step is to serve media files during development in your project's urls.py file. For that use static() function like this:-

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
# Your existing URL patterns
]

# Serve media files during development
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

* in the above code the static() function is used to serve media files during development.

*Now cross check your settings.py file that whether media handling is properly defined or not like this:-

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

* Now run your django development server and your issue will be resolved.
Larz60+ write Jul-20-2023, 11:28 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
Django images will not load - by pythonpaul32 - May-08-2023, 02:39 AM
RE: Django images will not load - by SpongeB0B - Jul-11-2023, 04:57 PM
RE: Django images will not load - by mactron - Jul-15-2023, 01:10 PM
RE: Django images will not load - by Gaurav_Kumar - Jul-20-2023, 10:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Fetching Images from DB in Django Dexty 2 1,875 Mar-15-2024, 08:43 AM
Last Post: firn100
  display local images on django website mp3909 2 6,453 Apr-01-2020, 11:18 AM
Last Post: mp3909
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,457 Jun-30-2019, 12:21 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020