尝试在 Django 模板中打印所有平面页时出现 DatabaseError

DatabaseError when trying to print all flatpages in Django template

提问人:soqqq 提问时间:1/8/2023 更新时间:1/8/2023 访问量:33

问:

我有一个 Django 应用程序,我刚刚添加了简单页面。我遇到的问题是打印出我创建的所有平面页的列表。我想这样做的原因是,我可以将它们打印在标题导航模板中,以便在我创建/删除它们时自动显示它们。否则,我将不得不手动更新 html 文件以添加新链接。

我已将其添加到我的 views.py

urlpatterns += [
    re_path(r'^(?P<url>.*/)$', views.flatpage),
]

现在,我正在使用简单页面默认模板作为我的测试文件。

错误:

DatabaseError at /aaa/
No exception message supplied
Request Method: GET
Request URL:    http://214.191.20.123/aaa/
Django Version: 3.2
Exception Type: DatabaseError
Exception Location: /home/django/parser/venv/lib/python3.8/site-packages/djongo/cursor.py, line 59, in execute
Python Executable:  /home/django/parser/venv/bin/python
Python Version: 3.8.10
Python Path:    
['/home/django/parser',
 '/home/django/parser/venv/bin',
 '/usr/lib/python38.zip',
 '/usr/lib/python3.8',
 '/usr/lib/python3.8/lib-dynload',
 '/home/django/parser/venv/lib/python3.8/site-packages']
Server time:    Sat, 07 Jan 2023 22:01:23 +0000

错误:

Environment:


Request Method: GET
Request URL: http://214.191.20.123/aaa/

Django Version: 3.2
Python Version: 3.8.10
Installed Applications:
['adobeparser',
 'cbs_parser',
 'segment_parser',
 'parsers',
 'crispy_forms',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'django.contrib.flatpages']
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',
 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware']


Template error:
In template /home/django/parser/parsers/templates/flatpages/default.html, error at line 25
   (Could not get exception message)
   15 :             <span class="sr-only">Toggle navigation</span>
   16 :             <span class="icon-bar"></span>
   17 :             <span class="icon-bar"></span>
   18 :             <span class="icon-bar"></span>
   19 :           </button>
   20 :           <a class="navbar-brand" href="#">Flatpages Example</a>
   21 :         </div>
   22 :         <div class="collapse navbar-collapse" id="menu">
   23 :           {% get_flatpages as flatpages %}
   24 :           <ul class="nav navbar-nav">
   25 :              {% for page in flatpages %} 
   26 :               <li><a href="/pages{{ page.url }}">{{ page.title }}</a></li>
   27 :             {% endfor %}
   28 :           </ul>
   29 :         </div>
   30 :       </div>
   31 :     </nav>
   32 :     <div class="container">
   33 :       {{ flatpage.content }}
   34 :     </div>
   35 :   </body>


Traceback (most recent call last):
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 857, in parse
    return handler(self, statement)
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 933, in _select
    return SelectQuery(self.db, self.connection_properties, sm, self._params)
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 116, in __init__
    super().__init__(*args)
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 62, in __init__
    self.parse()
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 152, in parse
    self.where = WhereConverter(self, statement)
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/sql2mongo/converters.py", line 27, in __init__
    self.parse()
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/sql2mongo/converters.py", line 119, in parse
    self.op = WhereOp(
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/sql2mongo/operators.py", line 476, in __init__
    self.evaluate()
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/sql2mongo/operators.py", line 465, in evaluate
    op.evaluate()
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/sql2mongo/operators.py", line 465, in evaluate
    op.evaluate()
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/sql2mongo/operators.py", line 258, in evaluate
    self.rhs.negate()

The above exception ('NoneType' object has no attribute 'negate') was the direct cause of the following exception:
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/cursor.py", line 51, in execute
    self.result = Query(
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 784, in __init__
    self._query = self.parse()
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 885, in parse
    raise exe from e

The above exception (

    Keyword: None
    Sub SQL: None
    FAILED SQL: SELECT "django_flatpage"."id", "django_flatpage"."url", "django_flatpage"."title", "django_flatpage"."content", "django_flatpage"."enable_comments", "django_flatpage"."template_name", "django_flatpage"."registration_required" FROM "django_flatpage" INNER JOIN "django_flatpage_sites" ON ("django_flatpage"."id" = "django_flatpage_sites"."flatpage_id") WHERE ("django_flatpage_sites"."site_id" = %(0)s AND NOT "django_flatpage"."registration_required") ORDER BY "django_flatpage"."url" ASC
    Params: (5,)
    Version: 1.3.6) was the direct cause of the following exception:
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute
    raise db_exe from e

The above exception () was the direct cause of the following exception:
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/contrib/flatpages/views.py", line 45, in flatpage
    return render_flatpage(request, f)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/contrib/flatpages/views.py", line 69, in render_flatpage
    return HttpResponse(template.render({'flatpage': f}, request))
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/template/base.py", line 170, in render
    return self._render(context)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/template/defaulttags.py", line 168, in render
    len_values = len(values)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/db/models/query.py", line 262, in __len__
    self._fetch_all()
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/db/models/query.py", line 51, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1169, in execute_sql
    cursor.execute(sql, params)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/django/parser/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/django/parser/venv/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute
    raise db_exe from e

Exception Type: DatabaseError at /aaa/
Exception Value: 

我尝试从默认模板中删除所有内容,以便在导航到平面页面时,我只能看到我从管理面板添加到该页面的任何内容。然后我把最低限度放在默认页面中

{% load flatpages %}
{% get_flatpages as flatpages %}
<ul>
    {% for page in flatpages %}
        <li><a href="{{ page.url }}">{{ page.title }}</a></li>
    {% endfor %}
</ul>

经过一番阅读,我还发现它必须在一个块中,所以我然后尝试了......{% get_flatpages as flatpages %}

{% load flatpages %}
{% block content %}
{% get_flatpages as flatpages %}
<ul>
    {% for page in flatpages %}
        <li><a href="{{ page.url }}">{{ page.title }}</a></li>
    {% endfor %}
</ul>
{% endblock %}

这仍然给我同样的错误。我不确定我必须做什么,或者MongoDB是否可以做到这一点?我只希望能够在管理面板中添加/更新/删除页面,而不必通过 SSH 连接到服务器并更新这些页面的导航链接。

python django mongodb djongo django-flatpages

评论


答: 暂无答案