I am trying to use django-autocomplete-light From this tutorial https://github.com/yourlabs/django-autocomplete-light/blob/master/docs/tutorial.rst
I Installed it with pip and added it to my settings file
INSTALLED_APPS = (
'dal',
'dal_select2',
for my tenant value
My tenant model is
class Tenant(CommonInfo):
version = IntegerVersionField( )
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
def __unicode__(self):
return u'%s %s %s ' % ("#", self.id,"first_name", self.first_name, "last_name")
In my autocomplete view:
from django.shortcuts import render
from dal import autocomplete
from client.models import Tenant
class TenantAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
# Don't forget to filter out results depending on the visitor !
if not self.request.user.is_authenticated():
return Tenant.objects.none()
qs = Tenant.objects.all()
if self.q:
qs = qs.filter(last_name__istartswith=self.q)
return qs
in autocomplete url
from django.conf.urls import url, include,patterns
from client import views
urlpatterns = [
url(
r'^tenant-autocomplete/$',
views.TenantAutocomplete.as_view(),
name='tenant-autocomplete',
),
]
and in form
class LeaseTenantForm(forms.ModelForm):
tenant = forms.ModelChoiceField(
queryset=LeaseTenant.objects.all(),
widget=autocomplete.ModelSelect2(url='tenant-autocomplete')
)
class Meta:
model = LeaseTenant
exclude = ['lease']
However after rendering in browser I don't see the input text field but the empty dropdown. in actual view
def tenant_new(request,pk,uri):
lease = get_object_or_404(Lease, pk=pk)
title = 'tenant'
uri = _get_redirect_url(request, uri)
if request.method == "POST":
form = LeaseTenantForm(request.POST)
if form.is_valid():
tenant = form.save(commit=False)
tenant.lease = lease
tenant.save()
messages.add_message(request, messages.SUCCESS, str(tenant.id) + "-SUCCESS Object created sucssefully")
return redirect(uri)
else:
form = LeaseTenantForm()
return render(request, 'object_edit.html', {'form': form, 'title': title, 'extend': EXTEND})
My template file
{% block title %}
Add/Edit {{ title }}
{% endblock title %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-12">
<h4>{{ title }}</h4>
<br><div class="center">
<form method="POST" class="form" action="" method="get">
<div class="form-group">
{% csrf_token %}
{{ form|crispy}}
{{ form.media }}
<BR><BR>
<button type="submit" class="btn btn-primary btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock content %}
So basically the autocomplete is not working . What should I validate to make it work?
I was running into the same problem. This was a good reference: django-autocompletion-light simple foreign key completion shows not editable drop-down widget
Make sure you are loading the jquery in your header file and that you use the
{{ form.media }}
tag for your form.Here is how I load the jquery in my header file:
Also, have you tried going to the
tenant-autocomplete/
url to see if you get the results on that page like it is mentioned in the tutorial https://django-autocomplete-light.readthedocs.io/en/master/tutorial.htmlIf you can't get results from the url that should be the first bug you fix.