[Fixed]-How to set image url of a div as its background in Django template

1👍

After quite a lot of search, I could solve it finally.
Thought this could be helpful to someone who come across this scenario with the Media uploads.

Settings.py should contain below for sure :

TEMPLATE_CONTEXT_PROCESSORS = (
'django.template.context_processors.debug',
'django.template.context_processors.request',
"django.core.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.media",  # Mandatory for Media file uploads
"django.core.context_processors.static",
)

MEDIA_ROOT = '//ipaddress or localhost/.../media/'
MEDIA_URL = '/media/'

urls.py

from django.conf import settings

if settings.DEBUG:
    urlpatterns += patterns('',
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', {
    'document_root': settings.MEDIA_ROOT}))

When calling in the template.. it should be like

{{ MEDIA_URL }}{{ object.imagepath }} 

and make sure that the media folder has read permissions to the user accessing the image object

Leave a comment