【发布时间】:2020-03-02 21:40:13
【问题描述】:
我想将w3school.com autocomplete functionality 添加到我的 django 表单字段之一。起初我使用了一个简单的 html 表单并且它可以工作,但是在实现了使用表单和模型的django-way 之后,我无法再让自动完成功能工作了。 这是我的代码的相关部分:
forms.py
class PlantForm(forms.ModelForm):
class Meta:
model = Plant
fields = ('plantname')
widgets = {
'plantname': forms.TextInput(attrs={'placeholder': 'Trage hier den Namen des gelieferten Produkts ein.',
'class': 'autocomplete',
'autocomplete': 'off'}),
models.py
class Plant(models.Model):
plantname = models.CharField(max_length=255)
main.js
const plants = ["Strauß", "Pepperoni", "Physalis", "Aster"];
window.onload = function () {
autocomplete(document.getElementById("id_plantname"), plants);
};
index.html
<link rel="stylesheet" href= "{% static 'xyz/index.css' %}">
<script src="{% static 'xyz/main.js' %}"></script>
<form autocomplete="off" method="post">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-primary">Eintragen</button>
</form>
css 和 js 代码是从w3schools tutorial 复制和粘贴的。 我想这与自动完成类有关。也许它在forms.py中分配错误?根据 django 文档和我的测试,main.js 中字段的 id 应该是正确的。希望你能帮助我,非常感谢。
【问题讨论】:
标签: django python-3.x django-models autocomplete django-forms