Autocomplete field in Django

有些话、适合烂在心里 提交于 2019-12-22 03:51:08

问题


I have models similar to the following:

class Country(models.Model):    
        name = models.CharField(max_length=50, unique=True)

class City(models.Model):
        name = models.CharField(max_length=50, unique=True)
        country = models.ForeignKey(Country)

and essentially I want to add a City to the database in my template. Before that, i should link it to the Country that already exists, so i want to use 'autocomplete field' in my template to get Country from DB

I have the following form defined:

class AddCityForm(forms.ModelForm):
    city_name = forms.CharField(max_length=100)
    country_name = forms.CharField(max_length=100)

In my template i have the forms like this :

<form action="/city/add" method="post">{% csrf_token %}
{{ add_city_form.as_p }}
<input type="submit" value="Submit" />
</form>

so is there any solution in django, to make the field 'country_name' autocompleted from the databese ?

cheers, newbie in django.


回答1:


Yes. You can check django-autocomplete out




回答2:


As time passes, Django applications rise and fall. There's a grid on djangopackages.org that compares various autocompletion solutions and provides hints on their development status: https://djangopackages.org/grids/g/auto-complete/



来源:https://stackoverflow.com/questions/15770587/autocomplete-field-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!