Search code examples
djangourldjango-2.1

Django: When passing a string with -,anything behind - get droped


I try to pass a string param on django,but when I try it,anything behind - get droped.

I'm using Django 2.1.7 on Python3.My OS is Mac OS Mojave 10.14.

I'm accessing http://127.0.0.1:8000/ruleset/a096abd9-3855-4a91-9336-1d7e66aa5323

def ruleSet(request,id_):
    return HttpResponse(id_)
urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'ruleset/(\w+)', view.ruleSet)
]

I except to get a096abd9-3855-4a91-9336-1d7e66aa5323

I got a096abd9 instead


Solution

  • it is happening because "\w" didn't match "-" char.

    You need to write regex which can match uuid.

    Try below

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('ruleset/<str:id_>', view.ruleSet)
    ]
    

    Check below document for info about the pattern in Django URLs https://www.webforefront.com/django/regexpdjangourls.html