Aldon
Aldon

Reputation: 867

django url patterns error , id not passed to view.

i try to create rest api, but got stuck on generating url.

urlpatterns = [
path('admin/', admin.site.urls),
url(r'^api/shop/', include(('shop.api.urls', 'shop'), namespace='api-shop',)), ]

also tried as

path('api/shop/', include(('shop.api.urls', 'shop'), namespace='api-shop',)),

in shop/api/urls.py i wrote

urlpatterns = [
url(r'^(P<id>\d+)/$', ShopRudView.as_view(), name='shop-rud')

]

and in shop/api/views.py i using drf library generics views to create

class ShopRudView(generics.RetrieveUpdateDestroyAPIView):
lookup_field = 'id'
serializer_class = ShopSerializer

def get_queryset(self):
    return Shop.objects.all()

and when I enter http://127.0.0.1:8000/api/shop/1 i get

ing the URLconf defined in untitled1.urls, Django tried these URL patterns, in this order: admin/ shop/ ^api/shop/ ^(P\d+)/$ [name='shop-rud'] The current path, api/shop/1, didn't match any of these.

How to fix that?

Upvotes: 0

Views: 995

Answers (2)

Biman Pal
Biman Pal

Reputation: 439

In Django 3.1.1

urlpatterns = [
    path('api/<int:id>/', views.EmployeeDetailsCBV.as_view()),
]

url: http://127.0.0.1:8000/api/3/

in Django 3 '(?Pd+)/$' won't work.

https://docs.djangoproject.com/en/3.1/intro/overview/

Upvotes: 1

Alasdair
Alasdair

Reputation: 308769

You are missing the ? in your regex:

url(r'^(?P<id>\d+)/$', ShopRudView.as_view(), name='shop-rud')

Or, if you want to use path:

path('<int:id>/', ShopRudView.as_view(), name='shop-rud')

Upvotes: 1

Related Questions