How Do I Use A Decimal Number In A Django URL Pattern?

前端 未结 4 1549
终归单人心
终归单人心 2021-01-11 17:52

I\'d like to use a number with a decimal point in a Django URL pattern but I\'m not sure whether it\'s actually possible (I\'m not a regex expert).

Here\'s what I wa

4条回答
  •  一整个雨季
    2021-01-11 18:22

    It can be something like

    urlpatterns = patterns('',
       (r'^item/value/(?P\d+\.\d{2})/$', 'myapp.views.byvalue'),
       ... more urls
    )
    

    url should not start with slash.

    in views you can have function:

    def byvalue(request,value='0.99'):
        try:
            value = float(value)
        except:
            ...
    

提交回复
热议问题