How to use django-autocomplete-light

How to use django_autcomplete_light to add autocomplete to a single field in a form. I have a model based form and I want to add autocomplete to the firstname field.

I have done the following:

Set django_autocomplete_light

INSTALLED_APPS changed:

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

Added it to urls.py, here is my urls.py:

from django.conf.urls import include, url from django.contrib import admin

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:

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 with:

class PersonForm(forms.ModelForm)

at

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:

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

And I imported all the necessary jquery files

But autocomplete does not appear. I have no mistakes. I followed the documentation tutorial.

I use python manage.py runserverto run the application.

EDIT:

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")),
]

.

+4
2

- , django-autocomplete-light models.CharField. form.py , :

autocomplete_light.TextWidget("PersonAutocomplete")

, 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"),
        }

, , , , __str__ __unicode__ . "" __unicode__:

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

, , , , , , , .

+2

, , . foreign key many-to-many ( , ). birth_country PersonForm :

//forms.py

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

Person birth_country .

, , dal 3 .

0

Source: https://habr.com/ru/post/1599569/


All Articles