简体   繁体   中英

Error 404 on get request from AJAX call on django views

I am getting a "Page Not for error" for the ajax request that is supposed to fetch data from the django views

Projectboard/dashboard.html

 <form action="view">
    <br>
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"></script>
<script>
$(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)
                    }
    });
  });
});
</script>

projectboard/urls.py from django.conf.urls import url, include

    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')
]

views.py

    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))

The error i am getting is

GET http://127.0.0.1:8000/projectboard/view/?fruit=apple 404 (Not Found)

Seems like your url patterns is wrong. Try to change it for something like this:

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')
]

Does that works?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM