[英]django: passing a url as a parameter
I want to pass a URL from javascript to a django view. 我想将一个URL从javascript传递到django视图。 I have the following urls.py
我有以下urls.py
--- urls.py ---
url(r'^show/(?P<url>\.+)$', 'myapp.views.jsonProcess'),
The view has the following declaration: 该视图具有以下声明:
--- views.py ---
def jsonProcess(request, url):
The URL I pass in javascript is as follows: 我在javascript中传递的URL如下:
url = 'http://127.0.0.1:8000/show/' + 'http://www.google.com';
window.open(url);
I get a "Page not Found (404)" error while matching the URL. 匹配URL时出现“找不到页面(404)”错误。 What am I missing?
我错过了什么? Any, and all pointers are welcome since I am hopelessly stuck!
任何,所有指针都是受欢迎的,因为我无可救药地卡住了! :(
:(
Firstly, you don't need to escape the dot if you want it to mean any symbol (in urls.py
). 首先,如果您希望它表示任何符号(在
urls.py
),则无需转义点。
url(r'^show/(?P<url>.+)$', 'myapp.views.json_process')
Secondly, use encodeURIComponent
to properly escape the parameter. 其次,使用
encodeURIComponent
来正确转义参数。
var url = 'http://127.0.0.1:8000/show/' +
encodeURIComponent('http://www.google.com')
By the way, don't use mixedCase for function names in Python: 顺便说一句,不要在Python中使用mixedCase作为函数名 :
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
函数名称应为小写,并根据需要用下划线分隔,以提高可读性。
Another note that might help in future: don't hardcode Django URLs in JavaScript. 另一个可能在将来有用的注意事项:不要在JavaScript中硬编码Django URL。 You can generate them dynamically either in your views :
您可以在视图中动态生成它们:
from django.core.urlresolvers import reverse
url = reverse(
'myapp.views.json_process',
kwargs={'url': 'http://www.google.com'}
)
Or in templates : 或者在模板中 :
{% url myapp.views.json_process url="http://www.google.com" %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.