Django的FilteredSelectMultiple小部件仅在登录时有效 [英] Django's FilteredSelectMultiple widget only works when logged in

查看:32
本文介绍了Django的FilteredSelectMultiple小部件仅在登录时有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ModelForm,其中使用了FilteredSelectMultiple小部件.当我以创建的超级用户身份登录时,它的工作原理非常好.但是,当未登录时,我看不到窗体中的小部件,只能看到所有项目的列表(如多选).所以我的问题是:为什么FilteredSelectMultiple在登录时工作正常,而在退出时却不在?

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

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'
),

如果需要任何其他代码,请告诉我.(我使用Django 1.8和Python 2.7)

Tell me if you need any other code. (I use Django 1.8 and Python 2.7)

注销后加载页面时,控制台显示以下内容:

When loading the page when logged out, the consoles displays the following :

jsi18n:SyntaxError:期望的表达式,得到了'<'

jsi18n : SyntaxError: expected expression, got '<'

jsi18n:SyntaxError:期望的表达式,得到了<"

jsi18n : SyntaxError: expected expression, got '<'

SelectFilter2.js:ReferenceError:插值未定义

SelectFilter2.js : ReferenceError: interpolate is not defined

当我以超级用户身份登录时,这些消息都不会出现.

None of these messages appear when I am logged in as the superuser.

如答案中所建议,我尝试将媒体类更改为:

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'),

哪个会导致AttributeError:

Which results in an AttributeError:

/p/my/url/form中的

AttributeError

AttributeError at /my/url/form

'tuple'对象没有属性'startswith'

'tuple' object has no attribute 'startswith'

[...]

异常位置:/path/to/virtualenv/local/lib/python2.7/site-packages/django/forms/widgets.py在absolute_path中,第74行

Exception Location: /path/to/virtualenv/local/lib/python2.7/site-packages/django/forms/widgets.py in absolute_path, line 74

我也尝试更改模板:

<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 :(

还有其他想法吗?

推荐答案

我不确定,但这可能是因为该网址位于admin auth下?

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'
)

因此,当不以管理员身份登录时,无法访问脚本 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'
)

这篇关于Django的FilteredSelectMultiple小部件仅在登录时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆