Unable to get MEDIA_URL from Django widget template

I am a new Djangoer and figuring out how to create a custom widget, my problem is not getting MEDIA_URL in my widget template, while the form uses MySelectWidget to get MEDIA_URL itself.

#
#plus_sign.html
#
<a href="" class="" id="id_{{ field }}">
    <img src="{{ MEDIA_URL }}images/plus_sign.gif" width="10" height="10" alt="Add"/>
</a>

^ cannot load {{MEDIA_URL}} into this widget template, so I can’t load the .gif image correctly :(

#
#custom_widgets.py
#
from django import forms

class MySelectMultiple(forms.SelectMultiple):

    def render(self, name, *args, **kwargs):
        html = super(MySelectMultiple, self).render(name, *args, **kwargs)
        plus = render_to_string("plus_sign.html", {'field': name})
        return html+plus

#
#forms.py
#
from django import forms
from myapp.custom_widgets.py import MySelectMultiple

class MyForm(forms.ModelForm):
contacts = forms.ModelMultipleChoiceField(Contact.objects, required=False, widget=MySelectMultiple)

#
#views.py
#

def AddContacts(request):
    if request.method == 'POST':
        form = MyForm(request.POST)

        if form.is_valid():
            cd = form.cleaned_data
            new = form.save()
            return HttpResponseRedirect('/addedContact/')
    else:
        form = MyForm()

    return render_to_response('shop/my_form.html', {'form': form}, context_instance=RequestContext(request))


#
#my_form.html
#
{% extends "base.html" %}

{% block content %}
   {{ form.contacts }}
{% endblock %}

Please let me know how to properly load the widget image. Thank you so much for all the answers.

+3
source share
4 answers

Actually the right way to do this is Widget Media .

Media, CSS , . <a> .

class MyWidget(TexInput):
  ...
  class Media:
    css = {
      'all': ('my_widget.css',)
    }

MEDIA_URL , django.conf.settings settings.MEDIA_URL.

from django.conf import settings

class MyWidget(TextInput):
  ...
  def render(self):
    return render_to_string('my_widget.html', {
      'MEDIA_URL': settings.MEDIA_URL,
      ...
    })
+1

RequestContext.

:

from django.template import RequestContext

def render(self, name, *args, **kwargs):
    html = super(MySelectMultiple, self).render(name, *args, **kwargs)
    context = RequestContext({'field': name})
    plus = render_to_string("plus_sign.html", context)
    return html + plus

, @czarchaic, , - TEMPLATE_CONTEXT_PROCESSORS ( ).

Docs.

+2

, settings.py

TEMPLATE_CONTEXT_PROCESSORS=(
    ...other processors,
    "django.core.context_processors.media",
)

, TEMPLATE_CONTEXT_PROCESSORS, , .

http://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors

+1

, , RequestContext, MEDIA_URL render_to_string.

:

context = RequestContext({'field': name})

{{field}} .

, MEDIA_URL, {{field}}. Media javascript CSS. , src , , .

def render(self, name, *args, **kwargs):
        html = super(SelectMultipleWithModalDialog, self).render(name, *args, **kwargs)        
        **context = RequestContext({})
        popup_plus = render_to_string("widgets/modal_dialog_plus_sign.html", {'field': name}, context_instance=context)**        
        return html + popup_plus

, , . .

0

Source: https://habr.com/ru/post/1741528/


All Articles