Django-Ajax:在数据中发送url参数

Django-Ajax:在数据中发送url参数,ajax,django,get,urlconf,Ajax,Django,Get,Urlconf,我试图通过Ajax向Django服务器发送“GET”和“POST”请求。 首先,我提供URLConf: url(r'^write_to_file/(?P<file_name>.*)/(?P<content>.*)/$',generalFunctions.write_to_file, name ='write_to_file'), 到目前为止,我对这种方法感到满意,但现在通过“data”变量传递文件名和内容对我来说很重要,出于某种原因,我得到了404错误 $.ajax({

我试图通过Ajax向Django服务器发送“GET”和“POST”请求。 首先,我提供URLConf:

url(r'^write_to_file/(?P<file_name>.*)/(?P<content>.*)/$',generalFunctions.write_to_file, name ='write_to_file'),
到目前为止,我对这种方法感到满意,但现在通过“data”变量传递文件名和内容对我来说很重要,出于某种原因,我得到了404错误

$.ajax({
        type: "GET",
        url: '/write_to_file/',
        data: {'file_name':file_name, 'content':content},

        success: function(data){ 
             alert ('OK');
        },
        error: function(){
            alert("Could not write to server file " + file_name) 
        }
    });
服务器端错误:

Not Found: /write_to_file/
[15/Apr/2016 14:03:21] "GET /write_to_file/?file_name=my_file_name&content=my_content HTTP/1.1" 404 6662
客户端错误:

jquery-2.1.1.min.js:4获取http://127.0.0.1:8000/write_to_file/?file_name=my_file_name&content=my_content 404(未找到)

你知道为什么吗?是ajax语法出了问题还是与URLConf有关

url(r'^write_to_file/(?P<file_name>.*)/(?P<content>.*)/$',generalFunctions.write_to_file, name ='write_to_file'),
我想这就是你想要的


我想这就是你想要的

好的,它不再是404错误,但是我有505错误,原因不在视图本身内部(我在函数的最开始处放置了一个断点,调试器没有到达该点)。现在的错误是TypeError:write_to_file()只接受3个参数(1个给定)[15/Apr/2016 14:20:48]“GET/write_to_file/?file_name=my_file_name&content=my_content HTTP/1.1”500 16010这真的应该很明显:如果你不再在URL中传递这些值,你需要将它们作为参数删除到函数中。是的,因此,如果您已经定义了将_写入_文件视图,则需要删除除请求之外可能已声明的任何参数。e、 g.def写入文件(请求、文件名、内容):。。。应该成为def write_to_file(request):…很抱歉,我太笨了,但是如果我删除它们,那么我如何从Django视图访问我通过Ajax数据传递的内容?!(在视图中)file_name=request.GET.GET('file_name')以及与内容类似的内容,也可以查看request.GET.getlist(),它不再是404错误,但我有505错误,原因不在视图本身内部(我在函数的最开始处放置了一个断点,调试器没有到达该点)。现在的错误是TypeError:write_to_file()只接受3个参数(1个给定)[15/Apr/2016 14:20:48]“GET/write_to_file/?file_name=my_file_name&content=my_content HTTP/1.1”500 16010这真的应该很明显:如果你不再在URL中传递这些值,你需要将它们作为参数删除到函数中。是的,因此,如果您已经定义了将_写入_文件视图,则需要删除除请求之外可能已声明的任何参数。e、 g.def写入文件(请求、文件名、内容):。。。应该成为def write_to_file(request):…很抱歉,我太笨了,但是如果我删除它们,那么我如何从Django视图访问我通过Ajax数据传递的内容?!(在视图中)file_name=request.GET.GET('file_name')和类似的内容,也可以查看request.GET.getlist()
url(r'^write_to_file/(?P<file_name>.*)/(?P<content>.*)/$',generalFunctions.write_to_file, name ='write_to_file'),
url(r'^write_to_file/$',generalFunctions.write_to_file, name ='write_to_file'),