gpt4 book ai didi

python - 属性错误: 'NoneType' object has no attribute '_consistency' when trying to show a list from database

转载 作者:行者123 更新时间:2023-12-01 05:27:11 25 4
gpt4 key购买 nike

我想显示 Cassandra 数据库中的列表,并使用 Django 的 html 模板显示它。当我尝试显示数据时,它显示: AttributeError: 'NoneType' object has no attribute '_consistency'

View .py:

def music_list(request):
files = Music.objects.all()
return render_to_response('music_list.html',{'files':files})

模型.py:

class Music(Model):
id = columns.UUID(primary_key=True)
title = columns.Text()
description = columns.Text()
filename = columns.Text()

Music_list.html

{% extends "master.html" %}

{% block title %}This is the title from main page{% endblock %}

{% block contents %}
<ul>
{% for f in files %}
{{ f.id }}
{% endfor %}
<li><a href="#">Item1</a></li>
<li><a href="#">Item2</a></li>
<li><a href="#">Item3</a></li>
</ul>
{% endblock %}

这是错误页面:

AttributeError at /music-list
'NoneType' object has no attribute '_consistency'
Request Method: GET
Request URL: http://127.0.0.1:8000/music-list
Django Version: 1.5.1
Exception Type: AttributeError
Exception Value:
'NoneType' object has no attribute '_consistency'
Exception Location: C:\Python27\lib\site-packages\cqlengine\connection.py in execute, line 239
Python Executable: C:\Python27\python.exe
Python Version: 2.7.4
Python Path:
['D:\\Developer Center\\PyCharm\\MusicStore',
'C:\\Python27\\lib\\site-packages\\pip-1.3.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\mysql_python-1.2.4-py2.7-win32.egg',
'D:\\Developer Center\\PyCharm\\MusicStore',
'C:\\Windows\\SYSTEM32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages',
'C:\\Python27\\lib\\site-packages\\PIL']
Server time: Mon, 13 Jan 2014 11:24:09 +0330
Error during template rendering

In template D:\Developer Center\PyCharm\MusicStore\templates\music_list.html, error at line 7
'NoneType' object has no attribute '_consistency'
1 {% extends "master.html" %}
2
3 {% block title %}This is the title from main page{% endblock %}
4
5 {% block contents %}
6 <ul>
7 {% for f in files %}
8 {{ f.id }}
9 {% endfor %}
10 <li><a href="#">Item1</a></li>
11 <li><a href="#">Item2</a></li>
12 <li><a href="#">Item3</a></li>
13 </ul>
14 {% endblock %}
15
Traceback Switch to copy-and-paste view

C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response
response = callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
D:\Developer Center\PyCharm\MusicStore\MusicStoreApp\views.py in music_list
return render_to_response('music_list.html',{'files':files}) ...
▶ Local vars
C:\Python27\lib\site-packages\django\shortcuts\__init__.py in render_to_response
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) ...
▶ Local vars
C:\Python27\lib\site-packages\django\template\loader.py in render_to_string
return t.render(Context(dictionary)) ...
▶ Local vars
C:\Python27\lib\site-packages\django\template\base.py in render
return self._render(context) ...
▶ Local vars
C:\Python27\lib\site-packages\django\template\base.py in _render
return self.nodelist.render(context) ...
▶ Local vars
C:\Python27\lib\site-packages\django\template\base.py in render
bit = self.render_node(node, context) ...
▶ Local vars
C:\Python27\lib\site-packages\django\template\debug.py in render_node
return node.render(context) ...
▶ Local vars
C:\Python27\lib\site-packages\django\template\loader_tags.py in render
return compiled_parent._render(context) ...
▶ Local vars
C:\Python27\lib\site-packages\django\template\base.py in _render
return self.nodelist.render(context) ...
▶ Local vars
C:\Python27\lib\site-packages\django\template\base.py in render
bit = self.render_node(node, context) ...
▶ Local vars
C:\Python27\lib\site-packages\django\template\debug.py in render_node
return node.render(context) ...
▶ Local vars
C:\Python27\lib\site-packages\django\template\loader_tags.py in render
result = block.nodelist.render(context) ...
▶ Local vars
C:\Python27\lib\site-packages\django\template\base.py in render
bit = self.render_node(node, context) ...
▶ Local vars
C:\Python27\lib\site-packages\django\template\debug.py in render_node
return node.render(context) ...
▶ Local vars
C:\Python27\lib\site-packages\django\template\defaulttags.py in render
len_values = len(values) ...
▶ Local vars
C:\Python27\lib\site-packages\cqlengine\query.py in __len__
self._execute_query() ...
▶ Local vars
C:\Python27\lib\site-packages\cqlengine\query.py in _execute_query
columns, self._result_cache = self._execute(self._select_query()) ...
▶ Local vars
C:\Python27\lib\site-packages\cqlengine\query.py in _execute
return execute(q, consistency_level=self._consistency) ...
▶ Local vars
C:\Python27\lib\site-packages\cqlengine\connection.py in execute
consistency_level = connection_pool._consistency ...
▶ Local vars

最佳答案

根据 documentation cqlengine 必须创建与 cassandra 服务器的连接,例如:

connection.setup(['127.0.0.1:9160']) #here example IP address and port is used

然后同步表格

from cqlengine.management import sync_table
sync_table(Music)

在connection.setup函数中,参数之一是“一致性”,例如:

def setup(
hosts,
username=None,
password=None,
max_connections=10,
default_keyspace=None,
consistency='ONE',
timeout=None):

默认值为“ONE”,请检查您的数据库连接代码是否传递了空值?

将表同步到数据库时是否遇到任何错误?

关于python - 属性错误: 'NoneType' object has no attribute '_consistency' when trying to show a list from database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21086365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com