Django的自动补光简单的外键完成显示不可编辑的下拉窗口小部件(django-autocomple

2019-09-29 18:30发布

我对国家的榜样:

    class Country(models.Model):
        name = models.CharField(max_length=200)

    def __str__(self):
        return self.name

这是通过在模型的UserDetails外键简称:

    class UserDetails(models.Model):

        ...
        country = models.ForeignKey(Country,verbose_name='Country',null=True, blank=True, default = None)

通过使用Django的自动完成光我一亮,在插入该国时,我有在写的第一个字母是完成一个文本组件。

因此,我已经准备了:

    class CountryAutocomplete(autocomplete.Select2QuerySetView):
        def get_queryset(self):

            qs = Country.objects.all()

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

        return qs

随着urls.py:

    app_name='shared'
    urlpatterns = [
        url(
            r'^country-autocomplete/$',
            CountryAutocomplete.as_view(),
            name='country-autocomplete',
        ),
    ]

而对于的UserDetails形成:

    class UserDetailsForm(forms.ModelForm):

        country = forms.ModelChoiceField(
            queryset=Country.objects.all(),
            widget=autocomplete.ModelSelect2(url='shared:country-autocomplete')
            )

        class Meta:
            model = UserDetails
            fields = [...,"country"]

作为一个模板,我已经使用:

{% load static %}


{% block content %}
<form method="post" action=".">
    {% csrf_token %}
    {{ user_form.as_p }}
    {{ user_details_form.as_p }}
    <input type="submit" value="Submit" />
</form>
{% endblock %}

{% block footer %}
    <script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
    <link rel="stylesheet" type="text/css" href="{% static 'autocomplete_light/select2.css' %}" />
    <script type="text/javascript" src="{% static 'autocomplete_light/jquery.init.js' %}"></script>
    <script type="text/javascript" src="{% static 'autocomplete_light/autocomplete.init.js' %}"></script>
    <script type="text/javascript" src="{% static 'autocomplete_light/select2.js' %}"></script>
    <script type="text/javascript" src="{% static 'autocomplete_light/vendor/select2/dist/css/select2.css' %}"></script>
    <script type="text/javascript" src="{% static 'autocomplete_light/vendor/select2/dist/js/select2.full.js' %}"></script>
    <script type="text/javascript" src="{% static 'autocomplete_light/forward.js' %}"></script>
    <script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
    {{ form.media }}
{% endblock %}

我复制所有的静态文件名为在项目的根“静态”文件夹中,我已经在现场设置中设置STATIC_URL =“/静态/”。 虽然做get调用下,runserver记录给出了静态文件:

[23/Oct/2016 17:44:59] "GET /accounts/register/ HTTP/1.1" 200 2344
[23/Oct/2016 17:44:59] "GET /static/admin/js/vendor/jquery/jquery.js HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/autocomplete.init.js HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/select2.js HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/select2.css HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/jquery.init.js HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/vendor/select2/dist/css/select2.css HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/vendor/select2/dist/js/select2.full.js HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/forward.js HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/admin/js/vendor/jquery/jquery.js HTTP/1.1" 304 0

当我送一个直接到达自动完成看法,我得到的结果是什么,我希望:

http://127.0.0.1:8000/shared/country-autocomplete/?q=u

{"pagination": {"more": false}, "results": [{"text": "USA", "id": 3}]}

但是,如果我去登记表,为国家领域,我得到这样一个小部件:

这是空的,无法进行编辑。 怎样才可以有一个文本的小工具,建议国名,同时输入的信吗?

Answer 1:

问题是,我导入的CSS的JavaScript文件,更改

<script type="text/javascript" src="{% static 'autocomplete_light/vendor/select2/dist/css/select2.css' %}"></script>

至:

<link rel="stylesheet" type="text/css" href="{% static 'autocomplete_light/vendor/select2/dist/css/select2.css' %}" />

自动完成的工作通缉。



文章来源: django-autocompletion-light simple foreign key completion shows not editable drop-down widget