简体   繁体   English

如何使用django-autocomplete-light

[英]How to use django-autocomplete-light

How do I use django_autcomplete_light to add autocomplete to one field in a form. 如何使用django_autcomplete_light将自动填充添加到表单中的一个字段。 I have a form which is based off of a model, and I want to add autocomplete to the firstname field. 我有一个基于模型的表单,我想在firstname字段中添加自动完成功能。

I have done the following so far: 到目前为止我做了以下事情:

Install django_autocomplete_light 安装django_autocomplete_light

Changed the INSTALLED_APPS: 更改了INSTALLED_APPS:

INSTALLED_APPS = (
  'autocomplete_light',
  'django.contrib.admin',
   ...

Added it to urls.py, here is my urls.py: 将它添加到urls.py,这是我的urls.py:

from django.conf.urls import include, url from django.contrib import admin 来自django.conf.urls的import include,来自django.contrib的url导入管理员

urlpatterns = [
    url(r"^admin/", include(admin.site.urls)),
    url(r"^app/", include("app.urls")),
    url(r"^autocomplete/", include("autocomplete_light.urls")),
    url(r"^.*$", include("app.urls")),
]

Created a file called autocomplete_light_registry.py and added the following: 创建了一个名为autocomplete_light_registry.py的文件,并添加了以下内容:

import autocomplete_light as al
from .models import *

al.register(Person,
    search_fields = ["^firstname"],
    attrs={
        "placeholder":"First name",
        "data-autocomplete-minimum-characters":1,
    },
    widget_attrs={
        "data-widget-maximum-values":4,
        "class":"modern-style",
    },
)

changed my PersonForm from: 改变了我的PersonForm:

class PersonForm(forms.ModelForm)

to: 至:

class PersonForm(autocomplete_light.ModelForm)

    class Meta:
        model = Person
        autocomplete_fields = ("firstname")

I also added the following line to the html page for the form: 我还将以下行添加到表单的html页面:

{% include 'autocomplete_light/static.html' %}

And I imported all the necessary jquery files 我导入了所有必要的jquery文件

But the autocomplete does not appear. 但是自动完成功能不会出现。 I don't get any errors. 我没有得到任何错误。 I followed the documentations tutorial. 我按照文档教程。

I am using python manage.py runserver to run the application. 我使用python manage.py runserver来运行应用程序。

EDIT: 编辑:

I changed the urlpatterns to (made the django-autocomplete-light url first): 我将urlpatterns更改为(首先制作了django-autocomplete-light url):

urlpatterns = [
    url(r"^autocomplete/", include("autocomplete_light.urls")),
    url(r"^admin/", include(admin.site.urls)),
    url(r"^app/", include("app.urls")),
    url(r"^.*$", include("app.urls")),
]

This did not solve the problem though. 但这并没有解决问题。

You have to do something different if you are using django-autocomplete-light to search models.CharField . 如果使用django-autocomplete-light搜索models.CharField则必须执行不同的操作。 In your form.py you need to add the following widget to the field you want add autocomplete to: 在form.py中,您需要将以下小部件添加到要添加自动完成的字段中:

autocomplete_light.TextWidget("PersonAutocomplete")

For example forms.py: 例如forms.py:

from django import forms
import autocomplete_light
from .models import *

class PersonForm(forms.ModelForm):
    """
    This form is to create new RTN's.  It does not include lastupdateddate and
    lastupdatedby, because these will be automatically filled out.
    """
    class Meta:
        model = Rtn
        autocomplete_fields = ("firstname")
        fields = ["firstname", "lastname", "age"]
        widgets = {
           "firstname":autocomplete_light.TextWidget("PersonAutocomplete"),
        }

You should note that what will appear in the autcomplete will be what you return in the __str__ or __unicode__ function in your models class. 您应该注意,自动完成中显示的内容将是您在模型类中的__str____unicode__函数中返回的__str__ In my example my Persons class had the following __unicode__ method: 在我的示例中,我的Persons类具有以下__unicode__方法:

def __unicode__(self):
  return "{0} {1}".format(self.firstname, self.lastname)

So what is shown in the text field that is being autocomplete will be the full name of the person not just the first name, and if you select one the full name is entered into the field. 因此,正在自动完成的文本字段中显示的内容将是该人员的全名,而不仅仅是名字,如果您选择一个,则在该字段中输入全名。

According to this tutorial, I think your code does not make sense. 根据教程,我认为您的代码没有意义。 You need a foreign key or many-to-many relation with another model (not just one as in your example) to autocomplete a form field. 您需要与另一个模型(不仅仅是示例中的一个)的foreign keymany-to-many关系来自动填充表单字段。 In the tutorial example, birth_country is autocompleted in PersonForm like this: 在教程示例中, birth_countryPersonForm自动完成,如下所示:

//forms.py

class PersonForm(forms.ModelForm):
class Meta:
    model = Person
    fields = ('__all__')
    widgets = {
        'birth_country': autocomplete.ModelSelect2(url='country-autocomplete')
    }

The form represents all Person fields and overwrites birth_country to be autocompleted. 表单代表所有Person字段并覆盖birth_country自动完成。

Furthermore, be told that the dal plugin is already in version 3 and very different from your code. 此外,被告知dal插件已经在版本3中,并且与您的代码非常不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM