0

my code look like this

urls.py:

from django.urls import path
from. import views
app_name ='graduates'
urlpatterns = [
    .
    .
    path('status_detail/<str:id>/', views.status_detail, name='status_detail'),
    
            ]

views.py:

def status_detail(request, id):
      
    return HttpResponse(id)

then I wanna use it like this somewhere in my code

 <a href=" {% url 'graduates:status_detail' graduate.id %}" class="btn text-secondary "></a>

And it works fine for strings those don't contain forward slash. But I want to pass id of student to urls that look like this A/ur4040/09, A/ur5253/09 and etc

Please help me how could I do this

redoc
  • 61
  • 8

2 Answers2

2

We can use default Path converters available in django

path('status_detail/<path:id>/', views.status_detail, name='status_detail'),

path - Matches any non-empty string, including the path separator, '/'.

redoc
  • 61
  • 8
0

Try changing your url into a regex:

url(r'^status_detail/(?P<str:id>\w+)/', views.status_detail, name='status_detail')

If url doesn't work because you have urls with same structure, use re_path:

re_path(r'^status_detail/(?P<str:id>\w+)/', views.status_detail, name='status_detail')

Don't forget to import re_path:

from django.urls import path, re_path, include

I hope this solves your problem.

Danny
  • 326
  • 3
  • 15
  • thanks for showing possible way. But it isn't working yet shows error `raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: "^detail/(?P\w+)/" is not a valid regular expression: bad character in group name 'str:id' at position 12` – redoc Jul 05 '21 at 08:19
  • My bad, the structure of a regex on urls is: **(?Ppattern)** so in this case might work: `url(r'^status_detail/(?P\w+)/', views.status_detail, name='status_detail')` – Danny Jul 05 '21 at 17:29