Getting django-ajax_selects to work in my search form in Django

461 Views Asked by At

I'm trying to make a search form that uses AJAX Select so that when I type an entry into a field, there will be a dropdown that contains a list of an attribute from all of the model objects that match the text typed in. Here is the relevant code:

models.py

class Student(models.Model):
    student_id = models.CharField(max_length=128, unique=True, null=True, blank=True)
    first_name = models.CharField(max_length=128)
    last_name = models.CharField(max_length=128)
    ssn = USSocialSecurityNumberField(null=False)
    gender = models.CharField(max_length=128, choices=GENDER_CHOICES)
    dob = models.DateField(auto_now=False, auto_now_add=False, db_column="date of birth")
    location = models.CharField(max_length=128, choices=LOCATION_CHOICES, default='south_plainfield')

lookups.py

@register('first_name')
class FirstNameLookup(LookupChannel):

    model = Student

    def get_query(self, q, request):
        return self.model.objects.filter(first_name__icontains=q).order_by('first_name')[:50]

    def format_item_display(self, obj):
        return u"<span class='first_name'>%s</span>" % obj.first_name

forms.py

class StudentSearchForm(forms.Form):
    student_id = forms.CharField(max_length=64, required=False, label="Student ID")
    ssn = USSocialSecurityNumberField(widget=forms.TextInput(attrs={'class': 'form-control'}), label="SSN",
                                      help_text="Format: xxx-xx-xxxx", required=False)
    #Pay attention to this
    first_name = AutoCompleteSelectMultipleField('first_name', max_length=128, required=False, label="First Name")
    last_name = forms.CharField(max_length=128, required=False, label="Last Name")
    course = forms.ChoiceField(choices=STUDENT_SEARCH_COURSES, required=False)
    location = forms.ChoiceField(choices=STUDENT_SEARCH_LOCATION_CHOICES, required=False)
    dob = forms.DateField(widget=DateInput(), required=False, label="Date of Birth")
    gender = forms.ChoiceField(choices=STUDENT_SEARCH_GENDER_CHOICES, required=False)

views.py

def search_student(request):
    form = StudentSearchForm()
    if request.method == 'GET' and request.GET != {}:
        form = StudentSearchForm(request.GET)

        if form.is_valid():
            cleaned_form = form.cleaned_data
            student = Student.objects.none()
            for key in cleaned_form:
                value = cleaned_form[key]
                if value == '' or value == None:
                    continue
                else:
                    if student:
                        student = student.filter(**{key: value})
                    else:
                        student = Student.objects.filter(**{key: value})

            table = StudentTable(student)
            RequestConfig(request, paginate={'per_page': 2}).configure(table)
            return render(request, 'students/search_student.html', {'form': form, 'table': table})
        else:
            print(form.errors)

    return render(request, 'students/search_student.html', {'form': form})

search_student.html

<div class="box-body">
    <form id="student_form" method="get" action="/students/search_student/">
        {% csrf_token %}
        <div class="row">
            {% for field in form %}
                <div class="col-lg-3 col-md-3">
                    <div class="form-group">
                        {{ field.errors }}
                        {{ field.label_tag }}
                        <br>
                        {{ field }}
                    </div>
                </div>
            {% endfor %}
        </div>

        <button type="submit" name="submit" class="btn btn-primary btn-lg btn-block">Search</button>
    </form>
</div>

I'm only doing the first name field for now. For some reason, whenever I go to the search student template I get this error:

Reverse for 'ajax_lookup' with arguments '()' and keyword arguments '{'channel': 'first_name'}' not found. 0 pattern(s) tried: []

I'm not sure what I'm doing wrong, since I followed the doc for django-ajax-selects to make my code. Could someone shed some light on this? Thanks.

EDIT: It appears that I'm getting this error because I forgot to add ajax select into the urls.py. But the dropdown itself is not showing up.

EDIT 2: The dropdown now works. But for some reason it's not registering into the query when I try to search.

1

There are 1 best solutions below

0
On

Okay, so apparently all I had to do was change the AutoCompleteSelectMultipleField to just AutoCompleteField in order to get what I want.