Django template image won't load

前端 未结 4 1804
轮回少年
轮回少年 2020-12-22 09:23

Image won\'t load. I see o broken image icon. I use the static tag. I also tried giving the absolute path and never worked.I used the alt tag but still i can only see the br

相关标签:
4条回答
  • 2020-12-22 09:37
    1. check you static path setting in setting.py file

      STATICFILES_DIRS = ( '/PATH/OF/YOUR/STATIC/DIRECTORY', )

    2. if your static directory name is "static" then your path should be '/path-before-static/static' and you static URL should be.

      STATIC_URL = '/static/'

    3. your images should be within the static folder .../static/img/...jpg

    4. In your template should be like this.

      {% load staticfiles %} {% static 'img/demo.jpg' %} or "{{ STATIC_URL }}img/demo.JPG"

    change setting.py file with This code

    STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
    ]
    
    0 讨论(0)
  • 2020-12-22 09:38

    Are you in a production or development environment?

    Can you access the /static/images/logo.png by typing it into the browser?

    When in development, where debug is on I add this to the end of the urls.py file:

    from django.conf import settings
    from django.conf.urls.static import static
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    
    if settings.DEBUG:
        urlpatterns += staticfiles_urlpatterns()
        urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)
    

    See here: https://docs.djangoproject.com/en/1.5/ref/contrib/staticfiles/#other-helpers

    0 讨论(0)
  • 2020-12-22 09:49

    Change:

    {% load static %} {# loads static tag #}
    

    to this:

    {% load static from staticfiles %}
    
    0 讨论(0)
  • 2020-12-22 09:49
    {% load static %} {# loads static tag #}
    
     <div id="header">
       {% block header %}
         <img src="{% static 'images/logo.png' %}" alt="alternative text" />
         #check your quotes inside image tag.
       {% endblock %}
    </div>
    
    0 讨论(0)
提交回复
热议问题