2010-04-06 52 views
2
(r'^/(?P<the_param>[a-zA-z0-9_-]+)/$','myproject.myapp.views.myview'), 

我該如何改變它,以便「the_param」接受一個URL(編碼)作爲參數?如何在Django中編寫此URL?

所以,我想傳遞一個URL給它。

mydomain.com/http%3A//google.com 

編輯:我應該刪除正則表達式,像這樣...然後它會工作嗎?

(R '^ /(P [*])/ $??', 'myproject.myapp.views.myview'),

回答

2

添加%.的字符類:

[a-zA-Z0-9_%.-] 

注意:您不需要在字符類內部轉義特殊字符,因爲特殊字符在字符集內失去其含義。 -如果不是用來指定一個範圍應該用斜槓或開頭(python 2.4.x2.5.x,2.6.x)或字符集(python 2.7.x)的末尾進行轉義,因此類似[a-zA-Z0-9_%-.]會拋出一個錯誤。

+0

http://rubular.com/可能是值得一試。 – jluebbert 2010-04-07 04:24:14

+0

jluebbert:不錯的網站。我也喜歡這個:http://www.txt2re.com/ – 2010-04-07 07:43:36

0

你至少需要這樣的東西:

(r'^the/uri/is/(?P<uri_encoded>[a-zA-Z0-9~%\._-])$', 'project.app.view'), 

這表達式應該匹配所有描述here

請注意,你的例子應該是mydomain.com/http%3A%2F%2Fgoogle.com(斜槓應該被編碼)。

0

我認爲你可以做到這一點:

(r'^(?P<url>[\w\.~_-]+)$', 'project.app.view')