Django 使用 AJAX 传递参数
我正在尝试将参数传递给 Django 视图,但我无法决定最好的方法是什么。
我正在进行 AJAX 调用,我的 javascript 代码是
url = "/review/flag/code/"+ code +"/type/" + type + "/content/" + content + "/";
$.getJSON(url, somefunction);
该调用的相应 url
(r'^rate/code/(?P<code>[-\w]+)/type/(?P<type>[-\w]+)/content/(?P<content>[-\w]+)/$', 'project.views.flag_review'),
我可以在视图中获取参数,这样
def rate_review(request,code,type,content):
....
我的问题是因为 content
来自 textarea
它可能涉及许多转义字符,并且像上面那样传递它会因正则表达式而导致问题。
如果我传递像 www.xx.com/review/rate?code=1&type=2&content=sdfafdkaposdfkapskdf... 这样的参数,有什么办法吗?
谢谢
I am trying to pass parameters to Django view but I couldn't decide what is the best way to do it.
I am making an AJAX call and my javascript code is
url = "/review/flag/code/"+ code +"/type/" + type + "/content/" + content + "/";
$.getJSON(url, somefunction);
The corresponding url for this call
(r'^rate/code/(?P<code>[-\w]+)/type/(?P<type>[-\w]+)/content/(?P<content>[-\w]+)/
And I can get the parameters in my view such that
def rate_review(request,code,type,content):
....
My question is because content
comes from a textarea
it may involve many escape chars and passing it like above causes problems due to regular expression.
Is there any way if I pass the parameters like www.xx.com/review/rate?code=1&type=2&content=sdfafdkaposdfkapskdf... ?
Thanks
, 'project.views.flag_review'),
And I can get the parameters in my view such that
My question is because content
comes from a textarea
it may involve many escape chars and passing it like above causes problems due to regular expression.
Is there any way if I pass the parameters like www.xx.com/review/rate?code=1&type=2&content=sdfafdkaposdfkapskdf... ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
content
变量是通过表单中的textarea
输入字段输入的,则您可能应该使用 HTTPPOST
方法来提交数据。你会得到类似的东西:If the
content
variable is input through atextarea
input field in a form, you should probably be using the HTTPPOST
method for submitting the data. You would get something like:当然,在您的
rate_review
函数中,您可以访问request.GET
并访问这些参数:Sure, in your
rate_review
function you can accessrequest.GET
and access those paramters: