django autocomplete_light默认小部件而不是自动完成小部件

时间:2022-01-03 02:11:32

I want to create a simple form with a simple input text field that uses autocomplete to help users fill out the input field

我想创建一个简单的表单,其中包含一个简单的输入文本字段,该字段使用自动完成功能来帮助用户填写输入字段

I'm trying to get django_autocomplete_light to work - I followed the instructions closely but the rendered form still has a simple "dropdown" (select) menu instead of a autocomplete textfield.. (with ALL cities in it which takes a long time to process)

我正在尝试让django_autocomplete_light工作 - 我仔细按照说明操作,但渲染的表单仍然有一个简单的“下拉”(选择)菜单而不是自动完成文本字段..(其中所有城市都需要很长时间才能处理)

I made all the necessary entries in settings.py and urls.py and all the javascripts get loaded.

我在settings.py和urls.py中创建了所有必要条目,并且所有javascripts都被加载了。

Funny thing: I already got it to work - instead of the dropdown menu there was a textfield with autocomplete functionality - then I tried something else and when I tried the former working solution again it didn't work anymore. I can't find my error :(

有趣的是:我已经开始工作 - 而不是下拉菜单中有一个具有自动完成功能的文本字段 - 然后我尝试了其他的东西,当我再次尝试以前的工作解决方案时,它不再起作用了。我找不到我的错误:(

This is autocomplete_light_registry.py:

这是autocomplete_light_registry.py:

   import autocomplete_light

   from cities_tiny.models import City

   autocomplete_light.register(City, search_fields=('name','name_ascii'),
       autocomplete_js_attributes={'placeholder': 'city name ..'})

This is forms.py:

这是forms.py:

from django import forms
import autocomplete_light
from models import Search

class SearchForm(forms.ModelForm):
    class Meta:
        widgets = autocomplete_light.get_widgets_dict(Search)
        model = Search

This is models.py:

这是models.py:

from django.db import models
from django.core import urlresolvers


class Search(models.Model):
    city = models.ForeignKey('cities_tiny.city', null=True, blank=True)


    def get_absolute_url(self):
        return urlresolvers.reverse('non_admin:widget_update', args=(self.pk,))

It would be OK if this works but I'd rather have JUST the form... I think I don't need the additional model "Search" because I already have "cities_tiny.city". The form should get its autocomplete values from the "City" model.

如果这样可行,但我宁愿只有表格......我想我不需要额外的模型“搜索”,因为我已经有了“cities_tiny.city”。表单应从“City”模型中获取其自动完成值。

I also found out that I can not use "CityAutocomplete" somewhere in the model like in the docs. It always says KeyError.

我还发现我不能像在文档中那样在模型中的某处使用“CityAutocomplete”。它总是说KeyError。

Thanks for your help.

谢谢你的帮助。

1 个解决方案

#1


1  

I think i found the answer :)

我想我找到了答案:)

I always got the following message when using registered autocomplete names:

使用已注册的自动完成名称时,我总是收到以下消息:

KeyError at /search/ 'CityAutocomplete'

localhost:8000/autocomplete/ showed me all the registered autocompletes but nevertheless those autocompletes were unusable

localhost:8000 / autocomplete /向我展示了所有已注册的自动填充功能,但是那些自动填充功能无法使用

in forms.py i had to

在forms.py我不得不

import autocomplete_light_registry

to get rid of the KeyError

摆脱KeyError


as i mentioned before i got it to work somehow but im quite sure i didn't import autocomplete_light_registry so if someone has a tip where the "real" error lies ... thank you very much

正如我之前提到的,我让它以某种方式工作,但我很确定我没有导入autocomplete_light_registry所以如果有人有一个提示“真正的”错误所在...非常感谢你

#1


1  

I think i found the answer :)

我想我找到了答案:)

I always got the following message when using registered autocomplete names:

使用已注册的自动完成名称时,我总是收到以下消息:

KeyError at /search/ 'CityAutocomplete'

localhost:8000/autocomplete/ showed me all the registered autocompletes but nevertheless those autocompletes were unusable

localhost:8000 / autocomplete /向我展示了所有已注册的自动填充功能,但是那些自动填充功能无法使用

in forms.py i had to

在forms.py我不得不

import autocomplete_light_registry

to get rid of the KeyError

摆脱KeyError


as i mentioned before i got it to work somehow but im quite sure i didn't import autocomplete_light_registry so if someone has a tip where the "real" error lies ... thank you very much

正如我之前提到的,我让它以某种方式工作,但我很确定我没有导入autocomplete_light_registry所以如果有人有一个提示“真正的”错误所在...非常感谢你