简体   繁体   English

Django - django-autocomplete-light设置如何

[英]Django - django-autocomplete-light setup how to

I am following the tutorial on how to setup django-autocomplete fields and Im struggling to get it working. 我正在按照如何设置django-autocomplete字段的教程,我努力让它工作。 Here's the tutorial: https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html 这是教程: https//django-autocomplete-light.readthedocs.io/en/master/tutorial.html

settings Installed Apps 设置已安装的应用

INSTALLED_APPS = (
    'dal',
    'dal_select2',
    'django.contrib.admin',

project urls.py 项目urls.py

from textchange.views import TextbookAutoComplete


urlpatterns = [
    url(r'^textbook-autocomplete$', TextbookAutoComplete.as_view(), name='textbook-autocomplete'),

HTML HTML

<form method="POST"> {% csrf_token %}
        {% for field in form3 %}
          {{ field }}
        {% endfor %}
      <input id="search" class="button" type="submit" value="Search Textbooks" name="Search"></input>
      </form>

Forms.py Forms.py

class Search(forms.ModelForm):
    longschool = forms.ModelChoiceField(
        queryset=Textbook.objects.all(),
        widget=autocomplete.ModelSelect2(url='textbook-autocomplete')
    )
    class_name = forms.ModelChoiceField(
        queryset=Textbook.objects.all(),
        widget=autocomplete.ModelSelect2(url='textbook-autocomplete')
    )
    isbn = forms.ModelChoiceField(
        queryset=Textbook.objects.all(),
        widget=autocomplete.ModelSelect2(url='textbook-autocomplete')
    )

    class Meta:
        model = Textbook
        fields = ('longschool', 'class_name', 'isbn')

Views.py Views.py

class TextbookAutoComplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        # Don't forget to filter out results depending on the visitor !
        if not self.request.user.is_authenticated():
            return Textbook.objects.none()

        qs = Textbook.objects.all()

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

        return qs

Jquery added Jquery补充道

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

When the form shows up in my html it's just three dropdowns without input fields (as in without anywhere to type). 当表单显示在我的html中时,它只有三个没有输入字段的下拉菜单(因为没有任何地方可以输入)。 Can anyone see what I am missing? 谁能看到我失踪的东西?

Any help would be greatly appreciated. 任何帮助将不胜感激。

I just ran into the same problem and was able to solve it. 我遇到了同样的问题并且能够解决它。 I had 2 issues. 我有2个问题。 I was forgetting to load {{form.media}} below the form that I loaded. 我忘记在我加载的表单下面加载{{form.media}}

Make sure that you run the python manage.py collectstatic command after installing the django-autocomplete-light package. 确保在安装django-autocomplete-light软件包后运行python manage.py collectstatic命令。 (This will copy the static files of this third party package into your own static folder). (这会将此第三方软件包的静态文件复制到您自己的静态文件夹中)。

django-autocomplete-light tried to load these static files form static/admin/js/vendor/ , but for some reason the files were not there and could not be loaded. django-autocomplete-light试图从static/admin/js/vendor/加载这些静态文件,但由于某种原因,文件不存在且无法加载。 I manually included the javascript and css files in the header of the template where my form with the autocomplete input was loaded and that finally worked: 我手动将javascript和css文件包含在模板的标题中,其中我的表单加载了自动完成输入,最终工作:

when loading the form in a template: 在模板中加载表单时:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
 <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
 <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.full.js"></script>
</head>
<body>
 {{ form.as_p }}
 {{ form.media }}
</body>

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

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