Django 使用 AJAX 传递参数

发布于 2024-11-17 03:34:48 字数 716 浏览 0 评论 0原文

我正在尝试将参数传递给 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 技术交流群。

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

发布评论

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

评论(2

平安喜乐 2024-11-24 03:34:48

如果 content 变量是通过表单中的 textarea 输入字段输入的,则您可能应该使用 HTTP POST 方法来提交数据。你会得到类似的东西:

 url = "/review/flag/code/"+ code +"/type/" + type;
 $.post(url, {'content': content}, somefunction, 'html');

 (r'^rate/code/(?P<code>[-\w]+)/type/(?P<type>[-\w]+)/
, 'project.views.flag_review'),

def rate_review(request,code,type):
    content = request.POST['content']
    ...

If the content variable is input through a textarea input field in a form, you should probably be using the HTTP POST method for submitting the data. You would get something like:

 url = "/review/flag/code/"+ code +"/type/" + type;
 $.post(url, {'content': content}, somefunction, 'html');

 (r'^rate/code/(?P<code>[-\w]+)/type/(?P<type>[-\w]+)/
, 'project.views.flag_review'),

def rate_review(request,code,type):
    content = request.POST['content']
    ...
兔小萌 2024-11-24 03:34:48

当然,在您的 rate_review 函数中,您可以访问 request.GET 并访问这些参数:

/rate/code/?foo=bar

def rate_review(request):
    request.GET['foo']

Sure, in your rate_review function you can access request.GET and access those paramters:

/rate/code/?foo=bar

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