I am a beginner in Django and am having an issue with a template rendering an image url from the database. The image paths for my other images work fine. hard coded also works fine rendering my other images.Ex:
<img class="img-responsive logo" src="{% static 'media/images/logo.png' %}" alt="logo" />
but when I use this format or hardcode in template:
{%for m in users%}
<img src="{% static 'media/{{m.image}}' %}" alt="{{m}}" />
{%endfor %}
I get no image rendered and the following error when I inspect element (chrome):
http://127.0.0.1:8000/static/media/%7B%7Bm.image%7D%7D
Failed to load resource: the server responded with a status of 404 (Not Found)
as you can see in the 404 error the "squiggly brackets" aren't being processed.
I should note that when I do:
{{m.image}}
the saved url in the database returns the path of the image as saved in the database. I used the ImageField() in my model.
my settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = ['/Users/coreygumbs/Documents/Ibhuku/IbhukuProject2/ibhuku/static/',]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static', 'media')
my root urls.py
urlpatterns = [
#HomeView class is the homepage template
url(r'^$', HomeView.as_view(), name='home'),
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('accounts.urls', namespace='accounts')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my template code
{% extends "_profilebase.html" %}
{% load staticfiles %}
{% block profile %}
<div class="accountsProfile">
{% for m in Users%}
<h3>{{m}}</h3>
{{m.first_name}} {{m.last_name}}<br/>
{{m.username}}<br/>
{{m.image}}<br/>
<img src="{% static 'media/{{m.image}}' %}" alt="{{m}}" />
{% endfor %}
</div>
{% endblock profile %}
I have also used "collectstatic"
any help or advice would be greatly appreciated. Thanks in advance.