Error 404 on get request from AJAX call on django views
我正在为Ajax请求获取"page not for error",该请求应该从Django视图中获取数据。
项目板/仪表板.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <form action="view"> Select your favorite fruit: <select id="mySelect"> <option value="apple" selected >Select fruit</option> <option value="apple">Apple</option> <option value="orange">Orange</option> <option value="pineapple">Pineapple</option> <option value="banana">Banana</option> </select> </form> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> $(document).ready(function(){ $("#mySelect").change(function(){ selected ="apple"; $.ajax({ type: 'GET', dataType: 'json', contentType: 'application/json; charset=utf-8', url: '/projectboard/view/', data: { 'fruit': selected }, success: function(result) { document.write(result) } }); }); }); |
项目板/urls.py从django.conf.urls导入url,包括
1 2 3 4 5 6 7 8 | from . import views urlpatterns = [ url(r'^disp.html$', views.index2, name='index2'), url(r'^view/(?P<id_remedio>\w+)/$', views.view, name='view'), url(r'view$', views.view, name='view') ] |
号
视图.py
1 2 3 4 5 6 7 8 9 | from django.http import HttpResponse def view(request): data="bleh" if request.method == 'GET': print (request.body) data = request.body return HttpResponse(json.dumps(data)) |
我得到的错误是
1 | GET http://127.0.0.1:8000/projectboard/view/?fruit=apple 404 (Not Found) |
。
似乎你的URL模式是错误的。尝试将其更改为如下内容:
1 2 3 4 5 6 7 | from . import views urlpatterns = [ url(r'^disp.html$', views.index2, name='index2'), url(r'^view/(?P<id_remedio>\w+)/$', views.view, name='view'), url(r'^view/$', views.view, name='view') ] |
这行吗?