Django - Custom inclusion template for MEDIA_URL?

I have the following custom inclusion tag:

from django.template import Library from django.db.models import Count register = Library() @register.inclusion_tag('projects/work_part.html', takes_context=True) def project_list(context): return {'projects':context['projects']} 

My settings are as follows:

 TEMPLATE_CONTEXT_PROCESSORS = ( 'django.contrib.auth.context_processors.auth', 'django.core.context_processors.debug', 'django.core.context_processors.i18n', 'django.core.context_processors.media', 'context_processors.default_processors', ) 

I need to access MEDIA_URL in the work_path.html template, but the context processors don't seem to apply to custom templates.

How do I access MEDIA_URL in a template tag? I saw this post: Access STATIC_URL from a custom inclusion template tag , but I am not using STATIC_URL, is there another set of tags that need to be downloaded?

+7
source share
3 answers

You can do the same (as with STATIC_URL ) using tempatetag {% get_media_prefix %}

+4
source

The get_media_prefix tag is static for those of us who were looking for "load media" ...

 {% load static %} ... <img class="img" src="{% get_media_prefix %}{{ obj.image }}" alt="{{ obj.name }}" /> 
+12
source

Or you can simply ignore those template tags and use the MEDIA_URL variable MEDIA_URL . All variables from settings.py are accessible from the HTML template.

 <img class="img" src="{{ MEDIA_URL }}{{ obj.image }}" alt="{{ obj.name }}" /> 
0
source

All Articles