0

I created an endpoint and registered it in urls.py. When I try to hit the endpoint I get a 404 error but on the 404 page the url is shown as one of the patterns django tried to match. Stumped.

api.py

class UpdateLast(viewsets.GenericViewSet, UpdateModelMixin):

    def get(self):
        return XYZModel.objects.all()

    def partial_update(self, request, *args, **kwargs):
        if request.user.is_superuser:
            with transaction.atomic():
                for key, value in request.data:
                    if key == 'tic':
                        XYZModel.objects.filter(ticker=request.data['tic']).filter(trail=True).update(
                            last_high=Case(
                                When(
                                    LessThan(F('last_high'),
                                             request.data['high']),
                                    then=Value(request.data['high'])),
                                default=F('last_high')
                            )
                        )

urls.py (this is inside the app)

router = routers.DefaultRouter()
router.register('api/dsetting', DViewSet, 'dsetting')
router.register('api/update-last-high', UpdateLast, 'update-last-high')

urlpatterns = [
    path('', include(router.urls))
]

urls.py (main)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('my_app.urls')),

When I hit the end-point api/update-last-high I get a 404 page.

On that page the exact end-point is shown as a pattern that django tried to match.

^api/update-last-high/(?P[^/.]+)/$ [name='update-last-high-detail']

^api/update-last-high/(?P[^/.]+).(?P[a-z0-9]+)/?$ [name='update-last-high-detail']

1
  • Why are you using the router in the app's urls.py?- why not just copy the same format as the main urls.py? Commented Oct 21, 2022 at 0:37

1 Answer 1

1
from rest_framework import routers
from . import views
from django.urls import path, include


router = routers.DefaultRouter()
router.register('api/dsetting', DViewSet, 'dsetting')
router.register('api/update-last-high', UpdateLast, 'update-last-high')

# you forgot to include the router.urls

urlpatterns = [
    path('', include(router.urls))
]
Sign up to request clarification or add additional context in comments.

2 Comments

Still getting the same error. I forgot to add that part here in the question. I have rechecked and added. Editing the question. The 404 error clearly shows the url I am looking for.
Very strange, if I change the viewsets.GenericViewSet to viewsets.ModelViewSet I can get to the endpoint. Time for some more reading.

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.