0

I have a ModelForm with a FilteredSelectMultiple widget

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

enter image description here

If not it does not appear

enter image description here

I get the following js errors :

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.

Uncaught ReferenceError: interpolate is not defined at Object.init (SelectFilter2.js:38) at SelectFilter2.js:233 at NodeList.forEach () at SelectFilter2.js:231

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

Amine Messaoudi
  • 2,141
  • 2
  • 20
  • 37

1 Answers1

0

This could be because you are accessing a script under the admin url:

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

You need to access the js outside the /admin/ location. Either copy the script to a folder not under admin/ and then change the location, or provide an url not under admin. For instance add to urls.py:

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

And then change the form script url to just:

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

Check Django's FilteredSelectMultiple widget only works when logged in

jay1189947
  • 201
  • 1
  • 2
  • 9