Django FilteredSelectMultiple not rendering on pag

2020-04-16 03:28发布

I am currently using Django version 1.11.2 and would like to use the FilteredSelectMultiple widget outside of the admin page.

This is my forms.py:

class TBDAuthGroupManageForm(forms.Form):
    permissions = forms.ModelMultipleChoiceField(queryset=Permission.objects.all(),
                                                required=True,
                                                widget=FilteredSelectMultiple("Permissions", is_stacked=False))

class Media:
    css = {'all': ('/static/admin/css/widgets.css',), }
    js = ('/admin/jsi18n/',)

def __init__(self, parents=None, *args, **kwargs):
    super(TBDAuthGroupManageForm, self).__init__(*args, **kwargs)

This is my views.py:

class TBDAuthGroupManageView(DetailView):
    model = TBDAuthGroup
    template_name = 'perms/tbdauthgroup_manage.html'

    def get_context_data(self, **kwargs):
    context = super(TBDAuthGroupManageView, self).get_context_data(**kwargs)
    context['form'] = TBDAuthGroupManageForm()
    return context

And this is my template:

{% extends "base.html" %}
{% load static %}
{% block css %}
    {{ form.media }}
    <script type="text/javascript" src="{% static 'js/jquery-3.1.1.min.js' %}"></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>
{% endblock %}
{% block content %}
    {{ form }}
{% endblock %}

However when I render it in the page, I only get this: enter image description here

and not this:enter image description here

I would love to know what I'm doing wrong and how I could fix this so my form looks like the admin form.

1条回答
Animai°情兽
2楼-- · 2020-04-16 04:10
{% block content %}
{{ form.media }}
<form>
  {{ form.permissions }}
</form>
{% endblock %}

try this in your template

查看更多
登录 后发表回答