尝试将 url 作为参数传递给 django 视图时出现问题

发布于 2024-10-10 18:05:07 字数 872 浏览 3 评论 0 原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我很坚强 2024-10-17 18:05:07

要解决额外的斜杠问题,您应该对 itemLink 参数进行 url 编码(即,在传递它之前,在该项目的 asolute url 定义中)

您可以使用 python urllib 库来执行此操作:

import urllib
#when defining the absolute url in get_absolute_url() for example
urllib.quote('http://www.google.ca/?q=django', safe='')
# which will output
>> 'http%3A%2F%2Fwww.google.ca%2F%3Fq%3Ddjango'

需要空安全参数,或者 urllib 不会对“/”进行编码
然后要在您的视图中解码它,您只需使用:

urllib.unquote(itemLink)

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 :

import urllib
#when defining the absolute url in get_absolute_url() for example
urllib.quote('http://www.google.ca/?q=django', safe='')
# which will output
>> 'http%3A%2F%2Fwww.google.ca%2F%3Fq%3Ddjango'

The empty safe parameter is needed, or urllib will not encode the "/"
Then to decode this in your view, you juste use :

urllib.unquote(itemLink)
胡大本事 2024-10-17 18:05:07

您可能希望将最后一部分中的网址正则表达式更改为 .{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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文