简体   繁体   中英

How to pass optional params in url in django python

i have 2 type of URL crud/app and other is crud/app/2 here you can see id can be optional, so how to apply this type of URL pattern, here i have added my code,

app_name = 'crud'
urlpatterns = [
    path('',views.index, name='index'),
    path('add/<:id>',views.add, name='add'),
]

urls.py

app_name = 'crud'
urlpatterns = [
    path('',views.index, name='index'),
    path('add',views.add, name='add_empty'),
    path('add/<int:id>',views.add, name='add_id'),
]

views.py

def add(request, id=None):

    # if id was provided
    if id:
        [...]
    else:
        [...]

But you shouldn't overwrite function names ( id ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM