尝试将 url 作为参数传递给 django 视图时出现问题
在我的 django 视图中,我有这样的内容:
def addOtherItemsForUserAndEvent(request, eventId, itemName, itemLink):
这与我的 urls.py 相匹配,其中我有这样的内容:
(r'^addOtherItemsForUserAndEvent/(?P<eventId>\d+)/(?P<itemName>\w{0,100})/(?P<itemLink>\w{0,500})/$', 'gatherings.views.addOtherItemsForUserAndEvent'),
这样做的目的是创建一个具有名称和项目 url 的项目,然后将该项目添加到事件中。我试图通过 ajax 调用来使用它,它可以工作,但很容易被破坏。
当我尝试将实际的 url 作为项目链接传递时,我的问题出现了,如下所示:
http://127.0.0.1: 8000/addOtherItemsForUserAndEvent/1/Pony/http://www.google.ca//
上面的示例应该创建一个名为 pony 的项目,其中包含指向 google 的链接,但由于额外的“,它与我的 url 不匹配” / 的”。看来我要么需要以某种方式修改我的 url 正则表达式,要么以某种方式对 url 进行编码或以不同的方式传递它......
任何帮助将不胜感激!
In my django view I have something like this:
def addOtherItemsForUserAndEvent(request, eventId, itemName, itemLink):
This matches up with my urls.py where I have this:
(r'^addOtherItemsForUserAndEvent/(?P<eventId>\d+)/(?P<itemName>\w{0,100})/(?P<itemLink>\w{0,500})/
The purpose of this is to create an item with the name and item url and then adds that item to an event. I'm trying to use this via an ajax call and it kind of works, but is very easily broken.
My problem pops up when I try to pass an actual url as the item link like this:
http://127.0.0.1:8000/addOtherItemsForUserAndEvent/1/Pony/http://www.google.ca//
The above example should create an item named pony with a link to google, but it doesn't match up with my url because of the extra "/'s". It seems like I either need to modify my url regex somehow, or else somehow encode the url or pass it differently....
Any help would be greatly appreciated!
, 'gatherings.views.addOtherItemsForUserAndEvent'),
The purpose of this is to create an item with the name and item url and then adds that item to an event. I'm trying to use this via an ajax call and it kind of works, but is very easily broken.
My problem pops up when I try to pass an actual url as the item link like this:
http://127.0.0.1:8000/addOtherItemsForUserAndEvent/1/Pony/http://www.google.ca//
The above example should create an item named pony with a link to google, but it doesn't match up with my url because of the extra "/'s". It seems like I either need to modify my url regex somehow, or else somehow encode the url or pass it differently....
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要解决额外的斜杠问题,您应该对 itemLink 参数进行 url 编码(即,在传递它之前,在该项目的 asolute url 定义中)
您可以使用 python urllib 库来执行此操作:
需要空安全参数,或者 urllib 不会对“/”进行编码
然后要在您的视图中解码它,您只需使用:
To solve the extra slashes problem, you should url-encode your itemLink parameter (that is, before passing it, in the asolute url definition of this item)
You can use the python urllib library to do this :
The empty safe parameter is needed, or urllib will not encode the "/"
Then to decode this in your view, you juste use :
您可能希望将最后一部分中的网址正则表达式更改为
.{0,500}
-\w
不会与您网址中的标点符号匹配。You probably want to change your URL regex to
.{0,500}
in the last part -\w
isn't going to match the punctuation in your URL.