2017-04-13 25 views
0

「を持っている」私はこのコードから次のエラーを取得する:はAttributeError:「SimpleCache」オブジェクトが属性を持っていない

def render(template, **kw):  
    if not cache.has("galleries"): 
     cache.set('galleries', getTable(Gallery)) 
    return render_template(template, galleries=galleries, **kw) 

エラー:

File "/vagrant/diane/diane.py", line 38, in render 
if cache.has("galleries"): 
AttributeError: 'SimpleCache' object has no attribute 'has' 

私はせずに何回か前に同じコードを使用していますあらゆる問題。私もこれをコピーし、簡単なテストを実行して動作します。

from werkzeug.contrib.cache import SimpleCache 
cache = SimpleCache() 

def x(): 
    if cache.has('y'): 
     print 'yes' 
     print cache.get("y") 
    else: 
     print 'no' 
x() 

すべてのアイデアは本当に感謝します。

+1

ドキュメント「このメソッドはオプションであり、すべてのキャッシュで実装されない可能性があります。これはdiane.pyで使用されているSimpleCacheのインスタンスに当てはまりますか? http://werkzeug.pocoo.org/docs/0.11/contrib/cache/ – JacobIRR

答えて

3

@JacobIRRのコメントからは、docからは、そのオプションのフィールドは明らかです。

次のようにドキュメントは言う:

ここ

has(key) Checks if a key exists in the cache without returning it. This is a cheap operation that bypasses loading the actual data on the backend.

This method is optional and may not be implemented on all caches.

Parameters: key – the key to check

この私たちがここで

get(key) Look up key in the cache and return the value for it.

Parameters: key – the key to be looked up. Returns: The value if it exists and is readable, else None. from werkzeug.contrib.cache import SimpleCache cache = SimpleCache()

は、我々はget(key)を使って何ができるかであるget(key)メソッドを使用することができ避けるために:

from werkzeug.contrib.cache import SimpleCache 
cache = SimpleCache() 

def x(): 
    if cache.get("y"): # if 'y' is not present it will return None. 
     print 'yes' 
    else: 
     print 'no' 
x() 
関連する問題