ajith siva
ajith siva

Reputation: 27

how to handle multiple keyword parameters in Django RestFramework URL

this my current url

url("sample/userid",views.sample)

I can query the data by userid or postid or x, or y parameters.

how to handle this efficiently in a single url

In the views, I will use kwargs to get the keywords, but not sure how to handle it in the url.

Upvotes: 0

Views: 318

Answers (2)

rahul.m
rahul.m

Reputation: 5854

Refer this https://docs.djangoproject.com/en/3.1/topics/http/urls/#example

from django.urls import path

path('sample/<int:parm1>/<int:parm2>/<slug:parm3>/', views.sample),

or 

url(r'^sample/(?P<parm1>[0-9]{4})/(?P<parm2>[0-9]{2})/(?P<parm3>[0-9]{2})/$', views.sample),

as query string

url(r'^sample/', views.sample)

http://127.0.0.1:8000/sample/?parm1=John&parm2=Susan

Upvotes: 0

Waket Zheng
Waket Zheng

Reputation: 6331

May be

def sample(request, *args, **kwargs):
    if userid := request.query_params.get('userid'):
        print('do sth')
    if postid := request.query_params.get('postid'):
        print('do other sth')
    ...

Curl this url: sample/userid?userid=v1&postid=v2

Upvotes: 0

Related Questions