django-autocomplete-light ModelSelect2Multiple 没有出现

问题描述

我的自动完成字段在管理页面显示并完美运行,但是当尝试从管理页面生成它时,它显示为一条细的垂直线。我该如何解决这个问题?

ModelSelect2Multiple field

forms.py

def student_form(request):

    if request.method == 'POST': 
        form = StudentForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('student_form')
    else: 
        form_class = StudentForm
        return render(request,'form.html',{'form': form_class})

class CourseAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        qs = CourseInstance.objects.all()

        if self.q:
            qs = qs.filter(course_name__istartswith=self.q)

        return qs

views.py

class StudentForm(autocomplete.FutureModelForm):
    first_name = forms.CharField(label= 'First Name ',widget=forms.TextInput)
    last_name = forms.CharField(label = 'Last Name ',widget=forms.TextInput)
    uni = forms.CharField(label = 'UNI ',widget=forms.TextInput)
    email = forms.CharField(label = 'Email ',widget=forms.TextInput)
    phone = forms.CharField(label = 'Phone number (optional)')

    time_zone = forms.ChoiceField(
        choices = TIME_ZONE_CHOICES,label = 'Time zone ')
    time_management = forms.ChoiceField(
        choices = TIME_MANAGEMENT_CHOICES,initial = 0,widget = forms.RadioSelect(attrs={'display': 'inline-block',}),label = 'How do you manage your time for assignments?')
    collaborative = forms.ChoiceField(
        choices = COLLABORATIVE_CHOICES,widget = forms.RadioSelect,label = 'How collaborative are you?')
    academic_serIoUsness = forms.ChoiceField(
        choices = SERIoUSnesS_CHOICES,label = 'How serIoUs of a student are you?')
    extroverted = forms.ChoiceField(
        choices = EXTROVERTED_CHOICES,label = 'How extroverted are you?')
    discovery = forms.MultipleChoiceField(choices = disCOVERY_CHOICES,widget = forms.CheckBoxSelectMultiple)
    fun_facts = forms.CharField(widget=Textarea)
    course_instances = forms.ModelMultipleChoiceField(
        queryset = CourseInstance.objects.all(),widget = autocomplete.ModelSelect2Multiple(url='course-autocomplete'))
    
    class Meta:
        model = Student
        fields = ['first_name','last_name','uni','email','phone','time_zone','time_management','collaborative','academic_serIoUsness','extroverted','discovery','fun_facts','course_instances']

urls.py

urlpatterns = [
    path('',views.index,name = 'index'),path('form/',views.student_form,#UpdateView.as_view(),name='student_form'),url(
        r'course-autocomplete/$',CourseAutocomplete.as_view(),name = 'course-autocomplete',),]

form.html

{% extends "base_generic.html" %}
{% load static %}

{% block content %} {# assuming you have a content block within your <body> element #}
    <form method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
    </form>
{% endblock %}

{% block footer %}

<script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
{{ form.media }}

{% endblock %} 
正如您所看到的,该字段在管理页面中似乎工作正常,所以我认为问题一定在于表单的呈现方式。

Admin page view

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)