Вопрос

I am new to django rest framework. I was wondering what will be the url pattern if I want to implement a GET api which has query params as key-value pairs. Something like this:

http://example.com/getResource?userid=<userid>&resourceid=<resourceid>

Could not find anything like this in django documentation. Please advise.

Thanks

Это было полезно?

Решение

For GET requests in Django (in general), you don't need to specify the parameters in the url pattern. In your urls.py, you simply write:

 url(r'^getResource$', 'app.views.view_function')

If your request url is (as in your example):

 http://example.com/getResource?userid=<userid>&resourceid=<resourceid>

You just get the values in the view function as follows:

 userid = request.GET['userid']
 resourceid = request.GET['resourceid']

If you're asking specifically about the django-rest-framework app, the docs (http://www.django-rest-framework.org/tutorial/quickstart) say your urls.py should be:

urlpatterns = patterns('',
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top