Django url pattern - 带斜杠的参数 [英] Django url pattern - parameter with forward slash

查看:212
本文介绍了Django url pattern - 带斜杠的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为两个参数创建一个网址格式,其中第一个参数包含正斜杠作为其内容的一部分:

How can I create a url pattern for two parameters where the first parameter contains a forward slash as part of its contents:

da/ta1=/data2

我有以下模式:

(r'^view/(?P<item_id>\w+=)/(?P<changekey>\w+)/$', 'view'),

但是,由于第一个正斜杠是参数数据

However this pattern does not match because of the first forward slash which is part of the parameter data.

推荐答案

假设您自己构建url,可以使用 quote_plus 来编码内联正斜杠:

Assuming you construct the url yourself, you could use quote_plus to encode the inline forward slash:

>>> '/'.join([urllib.quote_plus(d) for d in ['da/ta1', 'data2']])
'da%2Fta1/data2'

解码:

>>> urllib.unquote_plus('da%2Fta1/data2')
'da/ta1/data2'

为了匹配您的数据,您的模式可以更改为下面找到的结构。对于第一个参数,它将匹配所有内容,直到 = 字符;第二个参数预计是字母数字。

To then match your data, your pattern could be changed to the construct found below. For the first parameter, this matches everything up to the = character; the second parameter is expected to be alphanumerical.

(r'^view/(?P<item_id>[^=]+)=/(?P<changekey>\w+)/$', 'view')

这篇关于Django url pattern - 带斜杠的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆