Django 自动完成灯:未填充字段 [英] Django autocomplete light: field not populated

查看:34
本文介绍了Django 自动完成灯:未填充字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了Django autocomplete light",现在正在学习本教程:

问题:出生国家/地区"字段未填充,我也无法输入.

是什么原因?原因是否可能是由加载到 base.html 上的 .js 生成的?

在base.html中加载

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script>

解决方案

解决方案很简单:添加一些插件的javascrips和css

<link href="/static/collected/autocomplete_light/vendor/select2/dist/css/select2.css" type="text/css" media="all" rel="stylesheet"/><link href="/static/collected/autocomplete_light/select2.css" type="text/css" media="all" rel="stylesheet"/><script type="text/javascript" src="/static/collected/autocomplete_light/autocomplete.init.js"></script><script type="text/javascript" src="/static/collected/autocomplete_light/select2.js"></script>

I've installed "Django autocomplete light" and now following this tutorial: https://django-autocomplete-light.readthedocs.org/en/master/tutorial.html

Here is my code so far:

setting.py

INSTALLED_APPS = (
'dal',
'dal_select2',
'garages',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)

models.py

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

    def __str__(self):
        return self.name


class Person(models.Model):
    birth_country = models.ForeignKey(Country)

    def __str__(self):
        return self.birth_country

views.py

class CountryAutocomplete(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 Country.objects.none()
        qs = Country.objects.all()
        if self.q:
            qs = qs.filter(name__istartswith=self.q)
        return qs


def create_person(request):
    person_form = PersonForm()
    c = {
        'person_form': person_form,
    }
    c.update(csrf(request))

    return render_to_response('garages/person_form.html', c,
                              context_instance=RequestContext(request))

forms.py

class PersonForm(forms.ModelForm):
class Meta:
    model = Person
    fields = '__all__'
    widgets = {
        'birth_country': autocomplete.ModelSelect2(url='garages:country-autocomplete')
    }

urls.py

url(r'^country-autocomplete/$', CountryAutocomplete.as_view(), name='country-autocomplete'),
url(r'^new_person/$', views.create_person, name='create_person'),

person_form.html

{% extends 'base.html' %}

{% block content %}
<div>
    <form action="" method="post">
        {% csrf_token %}
        {{ person_form.as_p }}
        <input type="submit" />
    </form>
</div>
{% endblock %}

{% block footer %}
<script type="text/javascript" src="http://dal-yourlabs.rhcloud.com/static/collected/admin/js/vendor/jquery/jquery.js"></script>

{{ form.media }}
{% endblock %}


RESULT

The problem: 'Birth country' field is not populated and also I cannot type.

What's the reason? Is it possible that the cause is generated by .js loaded on base.html?

In base.html is loaded

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script>

解决方案

the solution was simple: adding some plugin's javascrips and css

<link href="/static/collected/autocomplete_light/vendor/select2/dist/css/select2.css" type="text/css" media="all" rel="stylesheet" />
<link href="/static/collected/autocomplete_light/select2.css" type="text/css" media="all" rel="stylesheet" />
<script type="text/javascript" src="/static/collected/autocomplete_light/autocomplete.init.js"></script>
<script type="text/javascript" src="/static/collected/autocomplete_light/select2.js"></script>

这篇关于Django 自动完成灯:未填充字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆