Django url pattern - string parameter

前端 未结 6 583
面向向阳花
面向向阳花 2021-02-06 21:10

Django url pattern that have a number parameter is:

url(r\'^polls/(?P\\d+)/$\', \'polls.views.detail\')

What will be the correct

6条回答
  •  你的背包
    2021-02-06 21:33

    Starting in Django 2.0 it is easier to handle string parameters in URLs with the addition of the slug symbol, which is used just like int in urls.py:

    from django.urls import path
    
    urlpatterns = [
        path('something/', views.slug_test),
    ]
    

    And in your function-based or class-based view you would handle it just like any other parameter:

    def slug_test(request, foo):
        return HttpResponse('Slug parameter is: ' + foo)
    

提交回复
热议问题