Reputation: 2535
I am working on an API in Django and one of my GET (Retrieve) URLs should follow the following format.
Ex:
In the retrieve method we pass the PK which is according to this URL is 151236 while keeping the section before to change between approve or decline. Is it possible to create a single API endpoint that dynamically change like in the above requirement without having to write two separate API endpoints for approve and decline?
Upvotes: 0
Views: 959
Reputation: 2342
In one of my project, I created dynamic urls with single endpoint. I think it was for django 1.11. Adding the code here.
urls.py
url(r'^settings/(?P<url>[.\w-]+)/$', settings_url_resolver, name='user_setting')
url_resolver
def settings_url_resolver(request, url=None):
url_path = request.path
if request.user.is_anonymous:
return redirect(reverse('user:login'))
if url_path == 'endpoint1':
view_name = SomeView.as_view()
return view_name(request)
elif url_path == 'endpoint2':
return view2
elif url_path == 'endpoint3':
return view3
else:
raise Http404
I wrote this couple of years ago. You can further improve the readability.
Upvotes: 2