简体   繁体   中英

Passing string parameters not working in Django

view.py

when i test my api with post man,postman indicate tha this urls do not exist(404 not found) any solution please

url postman:localhost:8000/photoByCategory/nature/

@api_view(['GET'])
@permission_classes((permissions.IsAuthenticated,))
def photo_by_categorie(request, category=False):
    if request.method == 'GET':
        if category:
            photo_by_cat = Photo.objects.filter(category=category)
            serializer = PhotoSerializer(photo_by_cat, many=True)
            return Response(serializer.data, status=status.HTTP_200_OK)

urls.py

re_path(r'photoByCategory/(?:(?P<category>\d+)/)?$', views.photo_by_categorie, name='photo_by_categorie'),

Well, I guess the problem is with your regex Try this

from django.urls import path, re_path

# Match string after drinks/ prefix
path('photoByCategory/<str:category>/',views.photo_by_categorie),
# Match one or more characters (non-digit regular expression) after drinks/ prefix
re_path(r'^photoByCategory/(?P<category>\D+)/',views.photo_by_categorie),

Here you can find the relevant information.

Note: I have ran first one myself and it works as intended

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM