Django - How can view function see difference of the endpoint being hit , without any value stated in the url?
I'm fairly new to Django and here's my case.
If i have 3 endpoints that i can't modify, and i need to point them to one same View function such as :
urls.py
urlpatterns = [
...
url(r'^a/', views.functionz.as_view(), name='a'),
url(r'^b/', views.functionz.as_view(), name='b'),
url(r'^c/', views.functionz.as_view(), name='c'),
...
]
If I'm restricted from changing the endpoints a/, b/, and c/ to something else that accepts parameters like xyz/a or xyz/b, how can my view function functionz identify the difference between them when it is being called?
Can I do something like this pseudocode?
views.py
Class XYZ(API View):
def post(self, request, format=None):
if request.endpoint == '/a/':
# do things if and only if the client hits /a/
I don't know if mapping diffrent url patterns to same view is a good idea, but if you want to do that logic with in the post you probably can use the get_full_path to get the current path and parse the last the item.
Class XYZ(API View):
def post(self, request, format=None):
current_path = request.get_full_path().rsplit("/", 1)
if current_path == 'a':
# Do something
if current_path == 'b':
# Do something