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]

0

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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."
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 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.
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')

7 Comments

yes that would work. however I checked this post and this is how it access the request data : stackoverflow.com/questions/24059536/…
otherwise my url would append the id but still can't find the url pattern
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
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')
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.
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.