Sustrak
Sustrak

Reputation: 115

pass multiple arguments in the url

I'm trying to make a simple django app that interacts with the database. But I'm having trouble passing more than one parameter in the url.

To pass the arguments i do it this way:

127.0.0.1:8000/REST/insert/?n=test/?x=2/?y=2/?z=True

And then in my views.py I get the parameters with:

name = request.GET.get('n', '')
x = request.GET.get('x', '')
y = request.GET.get('y', '')
z = request.GET.get('z', '')

Clearly I'm doing it wrong. How can I fix it ?

Upvotes: 0

Views: 3826

Answers (3)

osho paul
osho paul

Reputation: 3

First have to edit your urls.py file to accept the params you are sending, It should be,

url(r'^REST/insert/$', views.YourFuntion, name = 'your-fn'),

Second replace qustion marks and slashes // with & like this,

http://127.0.0.1:8000/REST/insert/?n=test&x=2&y=2&z=True

Upvotes: 0

Cagatay Barin
Cagatay Barin

Reputation: 3496

What you want to do is not safe. Since you do an insert operation, request type should be POST and you should send the information as json.

Just write the data as json and put it to the body of the request.

In your view;

import json
def your_view(request):
    body_unicode = request.body.decode('utf-8')
    data = json.loads(body_unicode) # This is a dictionary.

Upvotes: 1

Sayse
Sayse

Reputation: 43300

The separators between query parameters should be & characters instead of slashes and question marks (except the first).

/?n=test/?x=2/?y=2/?z=True

should be

/?n=test&x=2&y=2&z=True

Note: I'm not actually sure you should be using query parameters here since your url has "insert" in it. If you're really trying to insert things into a database then this should at least be done through a post request as post data.

Upvotes: 2

Related Questions