0

New to Python and Django and I'm trying to make a simple ajax call from a button click to pass certain data to my views.py, however, when I try to make a url as seen on my ajax code below, the documentId.id does not append unless I directly append in without the "?id=".

    {%for document in documents%}
       {{document.filename}}
       <input type="button" id="{{document.id}}" onclick="loadData(this)" name="load-data" value="Add"/>
    {%endfor%}

 <script type ="text/javascript">
    function loadData(documentId){
       $.ajax({
       url:"upload-data/load" + "?id=" + documentId.id,
       data: {'documentId': documentId},
       type: 'GET',
       success: function(){
          window.location.href = "http://127.0.0.1:8000/url/locations";
       }
    });
    }
 </script>

This gives me then an error that says the url cannot be found. I have a line in my urls.py below:

url(r^"upload-data/load/([0-9]+)/$', views.loadFile, name="load-data"),

Other than this method, I am stumped as to how I am going to extract my data to my views.py.

def loadFile(request):
    documentId = request.GET.get('id')
    newLayer = Layer(get_object_or_404(Document, pk = documentId))
    newLayer.save()
    layers = Layer.objects.all()

    return render(request, 'url/loaded.html', { 'layers': layers})

The persisting error in the console would be:

http://127.0.0.1:8000/upload-data/load/ [HTTP/1.0 404 Not Found]

Reiion
  • 923
  • 3
  • 15
  • 33

3 Answers3

2

Use something like this:

def loadFile(request):
    documentId= request.GET.get('id', '').
    newLayer = Layer(get_object_or_404(Document, pk = documentId))
    newLayer.save()
    layers = Layer.objects.all()

    return render(request, 'url/loaded.html', { 'layers': layers})

And update your url as :

    url(r^"upload-data/load/', views.loadFile, name="load-data")

And the script would be like :

<script type ="text/javascript">
    function loadData(documentId){
       $.ajax({
       url:"upload-data/load/?id="+ documentId.id,
       data: {'documentId': documentId},
       type: 'GET',
       success: function(){
          window.location.href = "http://127.0.0.1:8000/url/locations";
       }
    });
    }
 </script>

Thanks.

Prakhar Trivedi
  • 8,218
  • 3
  • 28
  • 35
  • Changed the following lines however same error occurs. if it helps, i checked network and the file said 1?documentId=1 where the params is definitely documentId:1. – Reiion Oct 19 '16 at 06:19
  • sorry for the late reply I had to check out the new error that occurred. It now says "TypeError: 'stepUp' called on an object that does not implement interface HTMLInputElement." – Reiion Oct 19 '16 at 06:42
  • yes probably I'll check up on this for a while! Thanks for the help. Although I would like to ask for an explanation why it had to be: documentId= request.GET.get('id', '')? I don't commonly see this. – Reiion Oct 19 '16 at 06:46
  • @Reiion In request.GET.get('id', ' ') The first value in the column is the value of id what you are getting from Javascript url, the second one is the default value, if id is null or empty. – Prakhar Trivedi Oct 19 '16 at 06:49
1

In JavaScript you need

"upload-data/load/" + documentId.id

Django doesn't use ?id= in url definition r^"upload-data/load/([0-9]+)/$'. It expects ie. upload-data/load/123 instead of upload-data/load?id=123


EDIT: and you need id in def loadFile(request, id).

And then you don't have to use request.GET.get('id')

furas
  • 134,197
  • 12
  • 106
  • 148
  • yes that would work. however I checked this post and this is how it access the request data : http://stackoverflow.com/questions/24059536/how-to-access-data-sent-in-ajax-call-in-views-py-python-django – Reiion Oct 19 '16 at 05:25
  • otherwise my url would append the id but still can't find the url pattern – Reiion Oct 19 '16 at 05:26
  • you use `^"upload-data/load/([0-9]+)/$` so Django expexts url with number ie. `upload-data/load/123`. If you use `^"upload-data/load/$` then it will expects `upload-data/load/` without number at the end. In both you can add `?id=` but first still will need number ie.`upload-data/load/123?id=123` – furas Oct 19 '16 at 05:36
  • btw: `^"upload-data/load/([0-9]+)/$ ` expects you have `def loadFile(request, id)` and then you don't have to use `request.GET.get('id')` – furas Oct 19 '16 at 05:39
  • Django doc: [URL dispacher](https://docs.djangoproject.com/en/1.10/topics/http/urls/) – furas Oct 19 '16 at 05:41
  • I was reading up on that documentation too but your explanation made it clearer. However the same thing happens. The get method url is still not found. The changes I made in javascript was to make it "/upload-data/load/" + documentId + "/?id=" + documentId just to be sure and had the same url line above. Deleted the line in view.py that you pointed out. – Reiion Oct 19 '16 at 06:02
  • 1
    try your url `http://127.0.0.1:8000/upload-data/load/123?id=123` directly in browser as address and you should see more information in browser - if you use debug mode in Django. – furas Oct 19 '16 at 06:07
1

From the above answers and comments it seems like rather than passing id as a url param you want to pass the same as a get param. In that case make your urls like below.

url(r^"upload-data/load/', views.loadFile, name="load-data")

and in views, check for get params by replacing id with documentId. document id will be in your dict named as data passed to view. So look for request.GET.get('data','') and from data extract id as below

def loadFile(request):
    data = request.GET.get('data', None)
    if data:
        documentId = data['documentId']
        newLayer = Layer(get_object_or_404(Document, pk = documentId))
        newLayer.save()
        layers = Layer.objects.all()

        return render(request, 'url/loaded.html', { 'layers': layers})
    else:
        return JsonResponse({'error': 'pass document id'}, status=400)

Since you are passing a get param from javascript named as documentId not id.

HTH

cutteeth
  • 2,148
  • 3
  • 25
  • 45