我想显示一个来自Cassandra数据库的列表,并在html模板usgin django中显示它。当我试图显示数据时,它说:AttributeError:'NoneType'对象没有属性'_consistency'
Views.py:
def music_list(request):
files = Music.objects.all()
return render_to_response('music_list.html',{'files':files})
Model.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
答案 0 :(得分:2)
根据cqlengine的documentation,必须创建与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函数中,其中一个参数是'consistency',如:
def setup(
hosts,
username=None,
password=None,
max_connections=10,
default_keyspace=None,
consistency='ONE',
timeout=None):
默认值为“ONE”,请检查您的数据库连接代码是否传递了任何空值?
在将表同步到数据库时是否出现任何错误?