0

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.

2 Answers 2

0

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
Sign up to request clarification or add additional context in comments.

2 Comments

I want that to be dynamic , say I am passing only user id or postid not both , how to handle that
url is deprecated in favor of re_path, also in row below you have URL ( case matters )
0

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

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.