django-autocomplete-light 模板不呈现自动完成小部件

django-autocomplete-light template not rendering autocomplete widget

我正在尝试在我的主页上创建一个搜索字段,您可以在其中按标签搜索条目,在该搜索字段中,当您输入字母时,它应该会建议您使用包含您目前输入的内容的标签。我正在使用 django-taggit 作为标签。 我已经按照本教程进行操作: https://django-autocomplete-light.readthedocs.io/en/master/taggit.html 它支持 django-taggit。

模板

    <div class="search-container">
        <form method="post">
            {% csrf_token %}
            {% for field in form %}
                  {{ field.label_tag }}
                  {{ field }}
            {% endfor %}
          <button type="submit">Search</button>
        </form>
    </div>

urls.py

# AUTOCOMPLETE URL
url(r'^tag-autocomplete/$', views.TagAutocomplete.as_view(), name='tag-autocomplete'),

forms.py

class SearchForm(autocomplete.FutureModelForm):

    class Meta:
        model = Intrare
        fields = ('tags',)
        widgets = {
        'tags': autocomplete.TaggitSelect2('intrari:tag-autocomplete')
    }

models.py

class Intrare(models.Model):
    tags = TaggableManager()

    def __str__(self):
        return self.titlu

views.py

class TagAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        intrari = [intrare.tags for intrare in Intrare.objects.filter(public=True)]
        tags = reduce(lambda x, y: x | y, [tags.get_queryset() for tags in intrari])
        tags = [tag.name for tag in tags]
        qs = Tag.objects.filter(name__in=tags)
        if self.q:
           qs = qs.filter(name__contains=self.q)
        return qs

Here is the result. The widget does not show properly

我发现错误了,我忘了在模板中添加{{ form.media }}。

   <div class="search-container">
        {{ form.media }}
        <form method="post">
            {% csrf_token %}
            {% for field in form %}
                  {{ field.label_tag }}
                  {{ field }}
            {% endfor %}
          <button type="submit">Search</button>
        </form>
    </div>