Home > Software engineering >  How to select a range of the data in urls to views in Django
How to select a range of the data in urls to views in Django

Time:11-27

path('<int:book_id>/',views.detail, name = 'detail')

How can I modify this line of code to can select the digits that can be used instead of sending any number?

as I want the range to be [1 - 5]

path('<int:book_id>[1-5]/',views.detail, name = 'detail'),

I've tried this but not correct

CodePudding user response:

You need to check it in the view itself.

def detail(request, book_id):
    if book_id < 1 or book_id > 5:
        # do something here when the number is not between or equals 1-5.
        raise PermissionDenied()

  • Related