I started a project awhile back and have made a lot of progress since then. In the beginning I didn't think much about restful architecture and how I'd serve data to the frontend. Now, however, it seems the way I am doing so isn't exactly like other projects I see, so here is my question.
What are the main issues with having my views formatted in such a way that when the frontend makes a request, the main view receives it and calls a function based on the action argument, rather than the frontend calling a different url endpoint? Here is an example below...
api.py
def to_return_stuff(request):
posts = Posts.objects.filter(~Q(user=request.user), is_active=True).order_by('-creation_date').values_list('description', 'pay', 'location', 'creation_date', 'user__username', 'pk', 'coords', 'listing_type', 'image', 'name')
posts = [p for p in posts if match(p[6], some_coords)]
return json.dumps(posts)
views.py
def home(request):
# page initially loads, just serving the static html, then the frontend triggers a fetch request, triggering the populate case, which serves data to the frontend.
...
...
if request.method == 'POST':
match request.POST.get("action"):
case "populate":
payload = to_return_stuff(request)
return JsonResponse(payload)
case "create post":
...
return HttpResponse(status=200)
case "remove post":
...
return HttpResponse(status=200)
case "report post":
...
return HttpResponse(status=200)
case "get favorites":
...
return JsonResponse({"some_data": some_data})
return render(request, 'dashboard.html')
Thank you.