Home > Software engineering >  How to make endpoint view in django to get queryset for autocomplete?
How to make endpoint view in django to get queryset for autocomplete?

Time:09-18

I am trying to make queryset for autocomplete search using jquery UI. I wrote a function based view which according to me should work ,but its not working as it should. when i type valid word it redirects me where search results should be shown but there i get error

AttributeError: 'Branches' object has no attribute 'FIELD'

Branches is my model

class Branches(models.Model):
    ifsc = models.CharField(primary_key=True, max_length=11)
    bank = models.ForeignKey(Banks, models.DO_NOTHING, blank=True, null=True)
    branch = models.CharField(max_length=250, blank=True, null=True)
    address = models.CharField(max_length=250, blank=True, null=True)
    city = models.CharField(max_length=50, blank=True, null=True)
    district = models.CharField(max_length=50, blank=True, null=True)
    state = models.CharField(max_length=26, blank=True, null=True)

my views.py

def search_ifsc(request):
    if request.is_ajax():
        q = request.GET.get('q', '').capitalize()
        search_qs = Branches.objects.filter(ifsc__startswith=q)
        results = []
        print(q)
        for r in search_qs:
            results.append(r.FIELD)
        data = json.dumps(results)
    else:
        data = 'fail'
    mimetype = 'application/json'
    return HttpResponse(data, mimetype)

urls.py

path('ajax/search/' , views.search_ifsc, name='search_view')

template

<div class="position-absolute top-50 start-50 translate-middle">
    <nav class="navbar navbar-light bg-light">
        <div class="container-fluid" class="ui-widget">
            <form id="search" method="POST" action="http://127.0.0.1:8000/ajax/search/">
                <!-- {% csrf_token %} -->
                <input type="text" class="form-control" id="q" name="q">
                <button type="submit" class="btn btn-default btn-submit">Submit</button>
                </form>
        </div>
    </nav>
</div>

<script type='text/javascript'>
        $(document).ready(function(){ 
            $("#q").autocomplete({
                source: "http://127.0.0.1:8000/ajax/search/",
                minLength: 2,
                open: function(){
                    setTimeout(function () {
                        $('.ui-autocomplete').css('z-index', 99);
                    }, 0);
                }
              });
        });

I get this error when i type and hit enter

Internal Server Error: /ajax/search/
Traceback (most recent call last):
  File "C:\Users\JAY SARDAR\projects\Django Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\JAY SARDAR\projects\Django Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\JAY SARDAR\projects\Django Angular\Fyle-assignment\Backend\restapi\rest_api\api\views.py", line 50, in search_ifsc
    results.append(r.FIELD)
AttributeError: 'Branches' object has no attribute 'FIELD'

as a beginner i have tried many tutorials to get the view work but its not enough. What is causing this error? note my admin , other pages are working fine.

CodePudding user response:

def search_ifsc(request):
    if request.is_ajax():
        q = request.GET.get('q', '').capitalize()
        search_qs = Branches.objects.filter(ifsc__startswith=q)
        results = []
        print(q)
        for r in search_qs:
            results.append(r.ifsc)
        data = json.dumps(results)
    else:
        data = 'fail'
    mimetype = 'application/json'
    return HttpResponse(data, mimetype)
  • Related