I have a ModelForm in which I use the FilteredSelectMultiple widget. It works perfectly fine when I'm logged in as the superuser I have created. However, when not logged in, I cannot see the widget in the form, I only see the list of all items (like a multiple select). So my question is : why is FilteredSelectMultiple working perfectly fine when logged in, but is not there when logged out ?
I haven't set any permission or anything like that anywhere that I can think of.
Here are parts of my code :
forms.py
from django.contrib.admin.widgets import FilteredSelectMultiple
class MyModelForm(forms.ModelForm):
my_field = forms.ModelMultipleChoiceField(queryset=Something.objects.all(), widget=FilteredSelectMultiple("Somethings", is_stacked=False), required=False)
class Media:
css = {
'all': (os.path.join(settings.BASE_DIR, '/static/admin/css/widgets.css'),),
}
js = ('/admin/jsi18n'),
class Meta:
model = MyModel
fields = ('some_field', 'some_other_field')
form.html
{% extends base.html %}
{% block head %}
{% load staticfiles %}
some stuff
{% endblock head %}
{% block content %}
<script type="text/javascript" src="{% url 'jsi18n' %}" > </script>
{{ form.media }}
<form enctype="multipart/form-data" method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Submit</button>
</form>
{% endblock content %}
urls.py
url(r'^admin/jsi18n/$',
'django.views.i18n.javascript_catalog',
name='jsi18n'
),
Tell me if you need any other code.
(I use Django 1.8 and Python 2.7)
EDIT
When loading the page when logged out, the consoles displays the following :
jsi18n : SyntaxError: expected expression, got '<'
jsi18n : SyntaxError: expected expression, got '<'
SelectFilter2.js : ReferenceError: interpolate is not defined
None of these messages appear when I am logged in as the superuser.
EDIT 2
As suggested in the answers, I tried changing my media class to :
class Media:
# Nécessaire pour l'affichage de FilteredSelectMultiple
css = {
'all': (os.path.join(settings.BASE_DIR, '/static/admin/css/widgets.css'),),
}
extra = '' if settings.DEBUG else '.min'
js = ('/admin/jsi18n', 'jquery%s.js' % extra, 'jquery.init.js', 'core.js', 'SelectBox.js', 'SelectFilter2.js'),
Which results in an AttributeError:
AttributeError at /my/url/form
'tuple' object has no attribute 'startswith'
[...]
Exception Location:
/path/to/virtualenv/local/lib/python2.7/site-packages/django/forms/widgets.py
in absolute_path, line 74
I also try changing my template:
<script type="text/javascript" src="{% url 'jsi18n' %}" > </script>
<script type="text/javascript" src="{% static 'admin/js/jquery.min.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/jquery.init.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/core.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/SelectBox.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/SelectFilter2.js' %}"></script>
{{ form.media }}
which didn't produce any error, but also didn't change anything to my problem :(
Any other idea ?
See Question&Answers more detail:
os