姜戈。自动完成...它不查找搜索

第五次

我是自动完成功能的 Django 新手。我正在尝试使用自动完成搜索。我正在关注本教程:https : //github.com/yourlabs/django-autocomplete-light/blob/master/docs/tutorial.rst

Django 不会给我任何错误。除了它实际上并没有在搜索中寻找结果之外,所有的工作都有效。相反,它给了我这个短语:“无法加载结果”

在 html 中,在开发人员部分它给了我以下错误:当第一次进入页面时 - GET http://dal-yourlabs.rhcloud.com/static/collected/admin/js/vendor/jquery/jquery.js net:: ERR_NAME_NOT_RESOLVED

在搜索字段中输入时:jquery.min.js:2 GET http://127.0.0.1:8000/sc_site/dirdict-autocomplete/?q=ghj 500(内部服务器错误)

我不使用 get 方法,因为我将搜索发布到数据库中......我使用的是 post 方法。我也尝试将此代码设置为:到模板中 -

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

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> 

就像这篇文章中所建议的那样:Django: django-autocomplete-light 不能正常工作但它并没有真正的帮助。我究竟做错了什么?谢谢!

更新:尝试将自动完成文件加载到项目中...做了以下操作:在 settings.py 中:

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

在此之后在 cmd 中:

python manage.py collectstatic

现在我在我的静态文件夹中看到一堆文件:“autocomplete.init.js、forward.js、jquery.init.js、jquery.post-setup.js、select2.css、select2.js”

但是我仍然看到上述错误:(。

我为这些 jquery 文件处理了一堆代码:在 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>

在 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 %}

我知道这是一团糟,我计划清理和更改一些地址,因为它们显然与我的项目无关,但我有一种讨厌的感觉,它无济于事:(

更新清理脚本和静态文件的链接。一般地址是错误的。还剩最后一个故障。我下载了firebug,但是一般不知道怎么处理里面的信息: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:
...

如果有人引导我朝着正确的方向发展,那就太好了。我浏览了许多 stackoverflow 论坛,但大多没有关于自动完成的答案,或者它们对我没有太大帮助......

第五次

最终的解决方案是在这个链接:Django Autocomplete Light - “结果无法加载”

这让我觉得解决方案很简单,但考虑到我对 django 并不完全熟悉,而且我第一次为 django 使用了自动完成功能,并加载了外部 css 和脚本文件,我认为合法地它可能会令人困惑?

无论如何,我要离开这个线程,所以它可能会帮助一些人走得更远......而且如果没有在整个解决方案中完成工作,最终链接将无法工作......

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章