Search code examples
djangodjango-formsdjango-viewsdjango-autocomplete-light

Django. autocomplete... It doesn't look up the search


I'm new to Django with autocomplete. I'm trying to use autocomplete search. I'm following this tutorial: https://github.com/yourlabs/django-autocomplete-light/blob/master/docs/tutorial.rst

Django doesn't give me any mistakes. All works except it doesn't actually looking for results in the search. Instead it gives me this phrase: "the results could not be loaded"

In html, in developer section it gives me following mistakes: When first get to the page - GET http://dal-yourlabs.rhcloud.com/static/collected/admin/js/vendor/jquery/jquery.js net::ERR_NAME_NOT_RESOLVED

When typing into the search field: jquery.min.js:2 GET http://127.0.0.1:8000/sc_site/dirdict-autocomplete/?q=ghj 500 (Internal Server Error)

I don't use get method, since I am posting searches into database... I am using post method. Also I tried to set this code: into the template -

<script type="text/javascript" src="http://dal-yourlabs.rhcloud.com/static/collected/admin/js/vendor/jquery/jquery.js"></script>

and

into the base.html-<link src="/static/collected/autocomplete_light/vendor/select2/dist/css/select2.css" type="text/css" media="all" rel="stylesheet" />
<link src="/static/collected/autocomplete_light/select2.css" type="text/css" media="all" rel="stylesheet" />
<script type="text/javascript" src="/static/collected/autocomplete_light/autocomplete.init.js"></script>
<script type="text/javascript" src="/static/collected/autocomplete_light/select2.js"></script> 

Like it was advised in this post: Django: django-autocomplete-light does not work properly But it doesn't really help. What am I doing wrong? Thank you!

Update: Tried to load autocomplete files into the project... Did following: In settings.py:

STATIC_ROOT = "C:/sc_project/sc/sc_site/static"
STATIC_URL = '/static/'

after this in cmd:

python manage.py collectstatic

Now I see bunch of files in my static folder: "autocomplete.init.js, forward.js, jquery.init.js, jquery.post-setup.js, select2.css, select2.js"

However I still see the above mistakes :(.

I processed bunch of code for these jquery files: in base.html:

<head>
  {% block title %}<title>СкрапОптТогрг</title>{% endblock %}
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  <!-- Add additional CSS in static file -->
  {% load static %}
  <link src="/static/collected/autocomplete_light/vendor/select2/dist/css/select2.css" type="text/css" media="all" rel="stylesheet" />
  <link src="/static/collected/autocomplete_light/select2.css" type="text/css" media="all" rel="stylesheet" />
  <script type="text/javascript" src="/static/collected/autocomplete_light/autocomplete.init.js"></script>
  <script type="text/javascript" src="/static/collected/autocomplete_light/select2.js"></script> 
  <!--------------------------------------------------->
  <link rel="stylesheet" href="{% static 'css/styles.css' %}">
  <link rel="stylesheet" type="text/css" href="{% static 'autocomplete_light/vendor/select2/dist/css/select2.css' %}" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
</head>

in template for search.html:

{% load static %}
<!--<script src="{% static 'admin/js/vendor/jquery/jquery.min.js' %}" type="text/javascript"></script>-->
<!--{% include 'autocomplete_light/static.html' %}-->
{% block css %}
    <!--<link rel="stylesheet" type="text/css" href="{% static 'autocomplete_light/vendor/select2/dist/css/select2.css' %}" />-->
    <link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}">
{% endblock %}
{% block content %}
<h2>Search</h2>
<form method="POST" class="form" action="" method="get">
    {% csrf_token %}

    {{form.as_p}}
    <script type="text/javascript" src="http://dal-yourlabs.rhcloud.com/static/collected/admin/js/vendor/jquery/jquery.js"></script>
    {{form.media}}
    <input type="submit" name="" value="Submit">
</form>
{% endblock %}
{% block footer %}
<script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
    <!--{{ form.media }}-->
<script>
(function($) {
    $('#add-form').click(function() {
        var index = $('#id_inline_test_models-TOTAL_FORMS').val()
        var newTable = $('#id_inline_test_models-__prefix__-DELETE').parents('table').clone()
        newTable.find(':input').each(function() {
            for (attr of ['name', 'id'])
                $(this).attr(
                    attr,
                    $(this).attr(attr).replace('__prefix__', index)
                )
        })
        newTable.insertBefore($(this))
        $('#id_inline_test_models-TOTAL_FORMS').val(
            parseInt($('#id_inline_test_models-TOTAL_FORMS').val()) + 1
        )
        newTable.slideDown()
    })
})($)
</script>
{% endblock %}

I do understand it is a mess and I am planning to clean up and change some addresses, since they are obviously not related to my project, but I have this nasty feeling that it wont help :(

UPDATE Cleaned up scripts and links to static files. Generally addresses were wrong. One last fault left. I downloaded firebug, but I generally don't know what to do with the information from it: message about fault: firebug-lite.js:19000 GET h

ttp://127.0.0.1:8000/sc_site/drdict-autocomplete/?q=%D0%BF%D1%80%D0%BE 500 (Internal Server Error)
message from firebug:
FieldError at /sc_site/drdict-autocomplete/
Related Field got invalid lookup: istartswith

Request Method: GET
Request URL: http://127.0.0.1:8000/sc_site/drdict-autocomplete/?q=%D0%BF%D1%80%D0%BE
Django Version: 3.0.5
Python Executable: C:\Program Files (x86)\Python38-32\python.exe
Python Version: 3.8.1
Python Path: ['C:\\sc_project\\sc', 'C:\\Program Files (x86)\\Python38-32\\python38.zip', 'C:\\Program Files (x86)\\Python38-32\\DLLs', 'C:\\Program Files (x86)\\Python38-32\\lib', 'C:\\Program Files (x86)\\Python38-32', 'C:\\Users\\me\\AppData\\Roaming\\Python\\Python38\\site-packages', 'C:\\Program Files (x86)\\Python38-32\\lib\\site-packages']
Server time: Tue, 9 Jun 2020 16:17:56 +0000
Installed Applications:
['dal',
 'dal_select2',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'sc_site']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback (most recent call last):
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\me\AppData\Roaming\Python\Python38\site-packages\dal\views.py", line 50, in dispatch
    return super(ViewMixin, self).dispatch(request, *args, **kwargs)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\views\generic\list.py", line 142, in get
    self.object_list = self.get_queryset()
  File "C:\sc_project\sc\sc_site\views.py", line 129, in get_queryset
    qs = qs.filter(name__istartswith=self.q)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\models\query.py", line 904, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\models\query.py", line 923, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\models\sql\query.py", line 1350, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\models\sql\query.py", line 1377, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\models\sql\query.py", line 1311, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\models\sql\query.py", line 1156, in build_lookup
    raise FieldError('Related Field got invalid lookup: {}'.format(lookup_name))

Exception Type: FieldError at /sc_site/drdict-autocomplete/
Exception Value: Related Field got invalid lookup: istartswith
Request information:
USER: E

GET:
q = 'jam'

POST: No POST data

FILES: No FILES data

COOKIES:
csrftoken = '...'
sessionid = '...'

META:
...

It would be nice if somebody boot me in the right direction. I looked through many stackoverflow forums, but there are mostly no answers about autocomplete or they didn't help me too much...


Solution

  • The final solution was at this link: Django Autocomplete Light - "The results could not be loaded"

    It amused me how solution is easy, but considering that I am not completely familiar with django and I used autocomplete for django for the first time, and loading outer css and scripts files, I think lawfully it could be confusing?

    Anyway, I am leaving this thread, so it might help somebody further down the road... Also without work done throughout this solution, final link wouldn't work...