39
你有一個接受的Django一個first_name
和last_name
網址:製作一個正則表達式的Django URL令牌可選
('^(?P<first_name>[a-zA-Z]+)/(?P<last_name>[a-zA-Z]+)/$','some_method'),
你會如何包括title
可選URL標記,沒有創建任何新的線路。我的意思是,在一個理想的方案:
#A regex constant
OP_REGEX = r'THIS IS OPTIONAL<title>[a-z]'
#Ideal URL
('^(?P<first_name>[a-zA-Z]+)/(?P<last_name>[a-zA-Z]+)/OP_REGEX/$','some_method'),
這是可能的,而無需創建即
('^(?P<first_name>[a-zA-Z]+)/(?P<last_name>[a-zA-Z]+)/(?P<title>[a-zA-Z]+)/$','some_method'),
新行
謝謝你。我將如何製作一個只有可選'標題'的URL?即?(?:/(?P [a-zA-Z] +))?(?:/(?P [a-zA-Z] +))?'感謝您的任何幫助 –
Federer
2010-02-24 15:19:01
請注意, :在外部組中很重要。沒有它,URL會在導航時正常工作,但reverse()不會注意到內部的參數。 – Chris 2013-01-28 19:56:04
反向使用此操作失敗,創建了2個url ... – radtek 2014-08-18 20:51:39