简体   繁体   English

django 在未登录的情况下使用管理小部件

[英]django use admin widget without being logged in

I have a ModelForm with a FilteredSelectMultiple widget我有一个带有FilteredSelectMultiple小部件的ModelForm

from django import forms
from django.contrib import admin

class PortFolioForm(forms.ModelForm):
    
    class Meta:
        model = PortFolio
        fields = ['title', 'description', 'keywords']
        
    class Media:
        css = {'all':['admin/css/widgets.css']}
        js = ['/admin/jsi18n/']

        
    keywords = forms.ModelMultipleChoiceField(label='Mots clés', widget=admin.widgets.FilteredSelectMultiple('Mots clés', False), queryset=Keyword.objects.all())

I'm using the form outside the admin, inside a view我在管理员之外的视图中使用表单

c = {'form': PortFolioForm() }
return render(request, 'portfolio.html', c)
......
form = PortFolioForm(request.POST)
if form.is_valid():
    form.save()
......
{{ form | crispy }}

When I'm already logged in as admin, the widget is dispayed as normal当我已经以管理员身份登录时,小部件正常显示

在此处输入图像描述

If not it does not appear如果没有,它不会出现

在此处输入图像描述

I get the following js errors:我收到以下 js 错误:

Refused to execute script from 'http://localhost/admin/login/?next=/admin/jsi18n/' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.拒绝从 'http://localhost/admin/login/?next=/admin/jsi18n/' 执行脚本,因为它的 MIME 类型 ('text/html') 不可执行,并且启用了严格的 MIME 类型检查。

Uncaught ReferenceError: interpolate is not defined at Object.init (SelectFilter2.js:38) at SelectFilter2.js:233 at NodeList.forEach () at SelectFilter2.js:231未捕获的 ReferenceError:在 SelectFilter2.js:233 的 NodeList.forEach () 的 SelectFilter2.js:231 的 Object.init (SelectFilter2.js:38) 处未定义插值

As the website I'm working on is meant to be used by other users, I would like to use just the widget itself, because all the saving is done within the views, using form.save()由于我正在开发的网站是供其他用户使用的,所以我只想使用小部件本身,因为所有的保存都是在视图中完成的,使用form.save()

This could be because you are accessing a script under the admin url:这可能是因为您正在访问管理员 url 下的脚本:

class Media:
    css = {'all':['admin/css/widgets.css']}
    js = ['/admin/jsi18n/']

You need to access the js outside the /admin/ location.您需要访问 /admin/ 位置之外的 js。 Either copy the script to a folder not under admin/ and then change the location, or provide an url not under admin.将脚本复制到不在 admin/ 下的文件夹,然后更改位置,或者提供不在 admin 下的 url。 For instance add to urls.py:例如添加到 urls.py:

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

And then change the form script url to just:然后将表单脚本 url 更改为:

class Media:
    css = {'all':['admin/css/widgets.css']}
    js = ['jsi18n/']

Check Django's FilteredSelectMultiple widget only works when logged in检查Django 的 FilteredSelectMultiple 小部件仅在登录时有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM