#jjango url中的字符

时间:2014-02-08 09:50:17

标签: python django python-2.7 python-3.x

我怀疑在django urls中使用。我知道用于在python中进行评论。 在做了Oauth后,我得到了一个像https://callbackurl.com/#some_characters这样的网址。 为了使用获取some_characters部分,我在我的网址配置文件中使用了以下代码。

url(r'^(.*)$', views.get_access_token, name="get_access_token")

我的观点如下:

def get_access_token(request,token_str):
    print token_str #prints to stdout

但我得到空字符串。 所以我的解释是python将之后的所有内容视为注释,即使在网址中也不是将其视为普通字符。 但是如果在python中使用简单的字符串,我们可以做类似的事情:

>>> name="anku#"
>>> name
'anku#'

此处被视为普通字符。 那么,如果有人想从网址中捕获https://someurl.com/sometext#anothertext#somemoretext,那么在网址是sometext#anothertext#somemoretext的情况下怎么办?我猜他需要相应地设计正则表达式命令,如果他对待sometext#anothertext#somemoretext作为特征,他只会得到sometext

上述语义是否不一致,因为url也是python字符串? 如果我错在某处,请纠正我。 问候。

1 个答案:

答案 0 :(得分:0)

当使用python传递OAuth的回调网址时,它应该是百分比编码的,因为〜和#之类的字符可能会导致问题。

网址:

https://someurl.com/sometext#anothertext#somemoretext

应该是

https://someurl.com/sometext%23anothertext%23somemoretext