0

On my Django website When I am making DEBUG = True Images are not loading. Please find my Dir structure and code.

Settings.py

    STATIC_DIR=os.path.join(BASE_DIR,'static')
    
STATIC_URL = '/static/admin/'

STATICFILES_DIRS=[
    STATIC_DIR,
]

MEDIA_URL = "/abcd/"
MEDIA_ROOT = BASE_DIR
    INSTALLED_APPS = [
    ------
    'django.contrib.staticfiles',
    -- - - - - -
]

Models.py

  class Product(models.Model):
    " " " 
     - -- - - -- - - 
    image = models.ImageField(upload_to='upload/products/', default='',null=True, blank=True)
    """""""""
class PostImage(models.Model):
    post = models.ForeignKey(Product, default=None, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='upload/products/')

urls.py from django.conf.urls.static import static

urlpatterns = [
    """"""",
   
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

HTML file:

  {% if product.image %}

                        <img class="rounded-circle" src="{{product.image.url}}" alt=".." height="45px" width="45px">
                        {% endif %}

here If I am changing Debug=False It is working as expected but when it is True images are not loading and in admin page CSS is not working.

Can anyone help me with steps on how to resolve this problem?

Shailesh
  • 135
  • 9
  • the issue has been resolved after referring the below document. - https://overiq.com/django-1-10/handling-media-files-in-django/#:~:text=The%20problem%20is%20that%20Django,in%20sitewide%20urls.py%20file.&text=Now%20visit%20http%3A%2F%2F127.0,able%20to%20see%20the%20image. – Shailesh May 13 '21 at 08:28
  • [You can find here another way to solve your problem](https://stackoverflow.com/questions/70129007/django-static-css-is-not-loading-on-page) – lord king Dec 09 '21 at 12:29
  • [To resolve your problem you can try:](https://stackoverflow.com/questions/70129007/django-static-css-is-not-loading-on-page) – lord king Dec 09 '21 at 12:31

0 Answers0