gpt4 book ai didi

jquery - Django 自动完成灯 : field not populated

转载 作者:行者123 更新时间:2023-11-28 18:32:05 24 4
gpt4 key购买 nike

我已经安装了“Django autocomplete light”,现在正在学习本教程:https://django-autocomplete-light.readthedocs.org/en/master/tutorial.html

到目前为止,这是我的代码:

设置.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',
)

模型.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))

表单.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 %}

结果

enter image description here

问题:“出生国家”字段未填充,我也无法输入。

这是什么原因?有没有可能是base.html上加载.js产生的原因?

在 base.html 中加载

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

最佳答案

确保在模板中包含 {{form.media}}。

关于jquery - Django 自动完成灯 : field not populated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36032372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com