Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

python - Django's FilteredSelectMultiple widget only works when logged in

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am not fully sure, but it might be because of the url is under admin auth?

url(r'^admin/jsi18n/$',
    'django.views.i18n.javascript_catalog',
    name='jsi18n'
)

So when not logged in as admin, the script jsi18n cannot be accessed because you need to be authenticated as admin to access the script.

Write the url to not be under admin:

url(r'^jsi18n/$',
    'django.views.i18n.javascript_catalog',
    name='jsi18n'
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...