0

I've Created a view

urlpatterns = [
   path('book/<suffix>/', views.bookview, name='bookDetail')`
]

what i want

  • if someone hits url '127.0.0.1:8000/book/1/2/3/4/5/6/'
  • i do not want django to raise an error of The current path, book/1/2/3/4/5/6/, didn’t match any of these.
  • but instead it shows the same view of url book/<suffix>/ which is view.bookview.
  • and somehow pass, suffix after book/ which is 1/2/3/4/5/6/ as arg so that i can access it in my view.
Verma
  • 173
  • 9

2 Answers2

0
from django.urls import re_path

urlpatterns = [
    re_path(r'^book/(?P<suffix>[\w/]+)$', views.bookview, name='bookDetail')
]
Verma
  • 173
  • 9
0

As there can be special characters in the suffix parameter, you need to encode the string. In python you can do it like this

from urllib.parse import quote_plus
suffix = quote_plus('/1/2/3/4/5/6/')
salmanwahed
  • 9,450
  • 7
  • 32
  • 55