Django's slug url regular match

 

If you follow  .* to match, it will last  /edit/ and  /delete/ also match inside.

urlpatterns = [
    #...
    url(r'^(?P<slug>.*)/$', post_detail, name='detail'),
    url(r'^(?P<slug>.*)/edit/$', post_update, name='update'),
    url(r'^(?P<slug>.*)/delete/$', post_delete),
]

Therefore, the use of  [-\w+] means to match  - the symbol (hyphen hyphen), and all lowercase letters.

urlpatterns = [
    #...
    url(r'^(?P<slug>[-\w]+)/$', post_detail, name='detail'),
    url(r'^(?P<slug>[-\w]+)/edit/$', post_update, name='update'),
    url(r'^(?P<slug>[-\w]+)/delete/$', post_delete),
]

 

 

reference:

https://www.jianshu.com/p/8207b66db9ca

https://www.cnblogs.com/yang-wei/p/9997776.html

 

 

 

Guess you like

Origin www.cnblogs.com/sea-stream/p/11518143.html