1

我想显示来自 Cassandra 数据库的列表并在 html 模板 usgin django 中显示它。当我试图显示数据时,它说: AttributeError: 'NoneType' object has no attribute '_consistency'

视图.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()

音乐列表.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
4

1 回答 1

2

根据 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”,请检查您的数据库连接代码是否传递了任何空值?

您在将表同步到数据库时遇到任何错误吗?

于 2014-01-13T12:25:58.483 回答