Django-autocomplete-url无效的URL

时间:2015-11-20 22:30:40

标签: python django autocomplete django-autocomplete-light

我正在尝试从机构列表中自动完成。我想我已经从django-autocomplete-light tutotial中包含了我需要的所有东西,但是我收到了这个错误并且不确定是什么导致它。希望你能帮忙。

错误是:

URL lookup for autocomplete 'InstitutionAutocomplete' failed. Have you included autocomplete_light.urls in your urls.py?

我的django-autocomplete-light文件夹与我的应用程序位于同一级别。还有另一个关于将它放在错误的目录中的线程,但似乎所有文件都可以正确导入。不知道如何测试这个。

这是我的urls.py:

import autocomplete_light
autocomplete_light.autodiscover()
admin.autodiscover()

url(r'^profile/add_user_accounts$', UserAccountsUpdate.as_view(), name='add_user_accounts'),
url(r'^profile/autocomplete$', include('autocomplete_light.urls'), name='autocomplete'),

这是我的app /目录中的auto_complete_light.py:

import autocomplete_light
from models import Institution

class InstitutionAutocomplete(autocomplete_light.AutocompleteModelBase):
    search_fields = ['^name', ]
    attrs={
        'placeholder': 'Find your institution',
        'data-autocomplete-minimum-characters': 1,
    }

autocomplete_light.register(Institution, InstitutionAutocomplete)

我的表格是:

class AddUserAccountsForm(forms.ModelForm):
    """
    Form for adding new user financial accounts.

    """
    required_css_class = 'required'
    user_institution_name = forms.ModelChoiceField(required=True, queryset=Institution.objects.all(), widget=autocomplete_light.ChoiceWidget('InstitutionAutocomplete'))
    user_accounts_name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'User Name'}), label="")
    user_accounts_password = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Password'}), label="")

    class Meta:
        model = User
        fields = ('user_institution_name','user_accounts_name', 'user_accounts_password', )

我的Html是:

{% load staticfiles %}

<form action="" method="get" role="form">
    <input id="InstitutionsSearch" type="text" name="q" placeholder="Search for your institution" autocomplete="off">
</form> 

<form action="" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Sign In" />
    <input type="hidden" name="next" value="{{ next }}" />
</form>

{% endblock %}

2 个答案:

答案 0 :(得分:2)

问题是您已使用“$”终止了包含的网址,因此之后没有任何内容可以匹配。将其更改为:

url(r'^profile/autocomplete', ...)

没有$。

请注意,最新版本的Django会在您执行此操作时发出警告。

答案 1 :(得分:0)

urls.py文件中,尝试更改此内容:

url(r'^profile/autocomplete$', include('autocomplete_light.urls'), name='autocomplete')

到此:

url(r'^profile/autocomplete$', include('autocomplete_light.urls', name='autocomplete'))

可能需要namespace代替name,我不确定。