python - How to set image url of a div as its background in Django template -


i have below code in template, dynamically set background url value come django view

<div class="profpic" style="background:url('{{ media_url }}{{ objects.profpicpath }}');background-size:cover"> </div> 

this gives output source code in browser below

<div class="profpic" style="background-image:url('/usersopr/documents/documents/20150718/bd55adcd-9360-4a05-b506-18373805b600_20150718_152906.jpg');background-size:cover"> </div> 

so,

{{ media_url }} rendered value '/usersopr/'  {{ objects.profpicpath }} value 'documents/documents/20150718/bd55adcd-9360-4a05-b506-18373805b600_20150718_152906.jpg' 

what else missing ?

here settings.py

media_root = '//192.xxx.xxx.xxx/d$/usersopr/' media_url = '/usersopr/' 

i searching extensively in internet on how set image url dynamically populated django view , rendered in django template, couldn't through - tried using {{ static_url }} , {{ media_url }} etc., , image path etc.,. couldn't get

after quite lot of search, solve finally. thought helpful come across scenario media uploads.

settings.py should contain below 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 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 template.. should

{{ media_url }}{{ object.imagepath }}  

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


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -