微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

django-autocomplete-light ModelSelect2Multiple 没有出现

如何解决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 举报,一经查实,本站将立刻删除。