2014-03-30 33 views
0

我需要能夠檢查鏈接是否有任何參數張貼或不張貼。GAE Python的request.get參數

例如:

www.hello.com/task www.hello.com/task?link=thislinke

我應該如何檢查是否有論據 「鏈接」 或不?

我試過,但沒有奏效:

if len(self.request.get_all()) > 0: 

這是當存在聯繫的說法

link = (self.request.GET['link']).encode('ascii','ignore') 

感謝我做什麼!

回答

1

方法self.request.get返回缺席參數一個空字符串,所以只檢查其結果爲空。

link = self.request.get('link') 
if link: 
    # do some work with link 
else: 
    # there is no 'link' argument 
+1

只是做「如果鏈接」,在這種情況下,我認爲更Python) – marcadian

+0

@marcadian你是對的:) –

2

http://webapp2.readthedocs.io/en/latest/guide/request.html

request = Request.blank('/test?check=a&check=b&name=Bob') 

# The whole MultiDict: 
# GET([('check', 'a'), ('check', 'b'), ('name', 'Bob')]) 
get_values = request.GET 

# The last value for a key: 'b' 
check_value = request.GET['check'] 

# All values for a key: ['a', 'b'] 
check_values = request.GET.getall('check') 

# An iterable with alll items in the MultiDict: 
# [('check', 'a'), ('check', 'b'), ('name', 'Bob')] 
request.GET.items()