Django使用AJAX传递参数

Django使用AJAX传递参数,django,django-views,parameter-passing,Django,Django Views,Parameter Passing,我正试图将参数传递给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]+)

我正试图将参数传递给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'),
我的问题是因为
content
来自
textarea
它可能涉及许多转义字符,并且像上面那样传递它会由于正则表达式而导致问题

如果我传递像www.xx.com/review/rate这样的参数,有什么办法吗?code=1&type=2&content=sdfafdkaposdfkapskdf


谢谢

当然,在您的
评分
功能中,您可以访问
请求。获取
并访问这些参数:

/rate/code/?foo=bar

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

如果
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']
    ...
url=“/review/flag/code/”+code+“/type/”+type;
$.post(url,{'content':content},somefunction,'html');
(r“^rate/code/(?P
[-\w]+)/type/(?P[-\w]+)/$”,“project.views.flag_review”),
def速率检查(请求、代码、类型):
content=request.POST['content']
...

这是一个很好的答案,谢谢,但我使用的方法类似于$.getJSON(url,{content:reason},somefunction);而不是.post。我如何才能在Jquery getJSON函数中实现将参数作为post方法传递?我发现:由于没有$.postJSON,如果您正在执行Jquery ajax post并希望得到JsonResult,则必须始终将“json”作为第四个参数传递给$.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']
    ...