class Thread(db.Model):
members = db.StringListProperty()
def user_is_member(self, user):
return str(user) in self.members
Run Code Online (Sandbox Code Playgroud)
和
thread = Thread.get(db.Key.from_path('Thread', int(id)))
is_member = thread.user_is_member(user)
Run Code Online (Sandbox Code Playgroud)
但错误是:
Traceback (most recent call last):
File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 511, in __call__
handler.get(*groups)
File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\util.py", line 62, in check_login
handler_method(self, *args)
File "D:\zjm_code\forum_blog_gae\main.py", line 222, in get
is_member = thread.user_is_member(user)
AttributeError: 'NoneType' object has no attribute 'user_is_member'
Run Code Online (Sandbox Code Playgroud)
为什么?
谢谢
这是我的HTML:
<div id='automail'>
<form action = "/admin/mail" method = "get">
auto mail when user :<br/><br/>
<div>
<input type="checkbox" name="automail" value ="signup">signUp</input><br/>
<input type="checkbox" name="automail" value ="login">login</input><br/>
</div>
<div style="text-align:right">
<input type="submit" value="save"></input>
</div>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我的python句柄:
class mail(BaseRequestHandler):
def get(self):
all=self.request.get('automail')
if not all:
self.response.out.write('sss')
return
self.response.out.write(all)
Run Code Online (Sandbox Code Playgroud)
当我选择'注册'和'登录'时,它只显示'注册'.
那么如何在gae上使用python从复选框中获取所有数据?
更新:
现在好了,有两种方法:
1.
all=self.request.get_all('automail')
2.
all=self.request.get('automail',allow_multiple=True)
如果我使用这个:
class A(db.Model):
a=db.StringProperty()
class demo(BaseRequestHandler):
def get(self):
a=A()
a.a='sss'
a.put()
raise Exception(a.key().id())
Run Code Online (Sandbox Code Playgroud)
我可以得到a.key().id()是961
但如果我添加key_name ="aaa",则a.key().id()将为None:
class A(db.Model):
a=db.StringProperty()
class demo(BaseRequestHandler):
def get(self):
a=A(key_name="aaa")
a.a='sss'
a.put()
raise Exception(a.key().id())
Run Code Online (Sandbox Code Playgroud)
那么当我设置key_name时,如何获得key().id()
谢谢
接下来是我的代码:
class foo:
def __init__(self):
self.a = "a"
def __getattr__(self,x,defalut):
if x in self:
return x
else:return defalut
a=foo()
print getattr(a,'b','sss')
Run Code Online (Sandbox Code Playgroud)
我知道__getattr__
必须是 2 个参数,但如果该属性不存在,我想获得一个默认属性。
我怎样才能得到它,谢谢
和
我发现如果定义了__setattr__
,我的下一个代码也无法运行
class foo:
def __init__(self):
self.a={}
def __setattr__(self,name,value):
self.a[name]=value
a=foo()#error ,why
Run Code Online (Sandbox Code Playgroud)
嗨亚历克斯,我改变了你的例子:
class foo(object):
def __init__(self):
self.a = {'a': 'boh'}
def __getattr__(self, x):
if x in self.a:
return self.a[x]
raise AttributeError
a=foo()
print getattr(a,'a','sss')
Run Code Online (Sandbox Code Playgroud)
它打印 {'a': 'boh'},而不是 'boh' 我认为它会打印 self.a 而不是 self.a['a'],这显然是不想看到的
为什么,有什么方法可以避免它
我在这段代码中看到一个字符串:
data[:2] == '\xff\xfe'
Run Code Online (Sandbox Code Playgroud)
我不知道'\ xff\xfe'是什么,
所以我想逃避它,但没有成功
import cgi
print cgi.escape('\xff\xfe')#print \xff\xfe
Run Code Online (Sandbox Code Playgroud)
我怎么才能得到它.
谢谢
这段代码是什么意思?
try:
import thread
except ImportError:
del _sys.modules[__name__]#why
raise
Run Code Online (Sandbox Code Playgroud)
但我找不到thread.py.
为什么,del _sys.modules[__name__]
我找不到谁定义了'__path__'
,为什么'__path__'
可以使用.
import os
import sys
import warnings
import ConfigParser # ConfigParser is not a virtualenv module, so we can use it to find the stdlib
dirname = os.path.dirname
distutils_path = os.path.join(os.path.dirname(ConfigParser.__file__), 'distutils')
if os.path.normpath(distutils_path) == os.path.dirname(os.path.normpath(__file__)):
warnings.warn(
"The virtualenv distutils package at %s appears to be in the same location as the system distutils?")
else:
__path__.insert(0, distutils_path)#who defined me.???
exec open(os.path.join(distutils_path, '__init__.py')).read()
Run Code Online (Sandbox Code Playgroud) class a(type):
def __str__(self):
return 'aaa'
def __new__(cls, name, bases, attrs):
attrs['cool']='cool!!!!'
new_class = super(a,cls).__new__(cls, name, bases, attrs)
#if 'media' not in attrs:
#new_class.media ='media'
return new_class
class b(object):
__metaclass__=a
def __str__(self):
return 'bbb'
print b
print b()['cool']#how can i print 'cool!!!!'
Run Code Online (Sandbox Code Playgroud) 获取Django,'simplejson'或其他方面的JSON数据的最佳JSON库是什么?
非常感谢
from functools import wraps
def a():
a='aa'
def b():
b="bbb"
c=wraps(a)(b)
print c#what happen?
Run Code Online (Sandbox Code Playgroud)
什么是包裹意味着,例子是最好的.