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

10,497

Solution 1

It can be something like

urlpatterns = patterns('',
   (r'^item/value/(?P<value>\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:
        ...

Solution 2

I don't know about Django specifically, but this should match the URL:

r"^/item/value/(\d+\.\d+)$"

Solution 3

If the values to be accepted are only $0.01 or $0.05, the harto's pattern may be specified like this:

r"^/item/value/(\d\.\d{2})$"

Solution 4

Don't use »

url(r"^item/value/(?P<dollar>\d+\.\d{1,2})$", views.show_item, name="show-item"),

It will only match the URL patterns like /item/value/0.01, /item/value/12.2 etc.

It won't match URL patterns like /item/value/1.223, /item/value/1.2679 etc.

Better is to use »

url(r"^item/value/(?P<dollar>\d+\.\d+)$", views.show_item, name="show-item"),

It will match URL patterns like /item/value/0.01, /item/value/1.22, /item/value/10.223, /item/value/1.3 etc.

Finally you can design your views.py something like

This is just for an example.

# Make sure you have defined Item model (this is just an example)
# You use your own model name
from .models import Item 

def show_item(request, dollar):
    try:
        # Convert dollar(string) to dollar(float).
        # Which gets passed to show_item() if someone requests 
        # URL patterns like /item/value/0.01, /item/value/1.22 etc.
        dollar = float(dollar);

        # Fetch item from Database using its dollar value
        # You may use your own strategy (it's mine)
        item = Item.objects.get(dollar=dollar);

        # Make sure you have show_item.html.
        # Pass item to show_item.html (Django pawered page) so that it could be 
        # easily rendered using DTL (Django template language).
        return render(request, "show_item.html", {"item": item});
    except:
        # Make sure you have error.html page (In case if there's an error)
        return render(request, "error.html", {});
Share:
10,497
Jason Champion
Author by

Jason Champion

I dig C++, Linux, Qt, wxWidgets, SDL2, Python, Django, and PostgreSQL. Interested in desktop audio software/tools.

Updated on June 04, 2022

Comments

  • Jason Champion
    Jason Champion about 2 years

    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 want to use for URLs:

    /item/value/0.01
    /item/value/0.05
    

    Those URLs would show items valued at $0.01 or $0.05. Sure, I could take the easy way out and pass the value in cents so it would be /item/value/1, but I'd like to receive the argument in my view as a decimal data type rather than as an integer (and I may have to deal with fractions of a cent at some point). Is it possible to write a regex in a Django URL pattern that will handle this?

  • Boldewyn
    Boldewyn almost 15 years
    If you want to have ints and floats in one url, write something like this: r"^/item/value/(\d+(?:\.\d+))$"
  • Jason Champion
    Jason Champion almost 15 years
    Thank you, each post added a little piece of the puzzle. Evgeny had the magic bullet.
  • btk
    btk about 12 years
    +1 for a more generic answer (not limited to numbers with two digits after decimal)
  • S.Ali
    S.Ali almost 11 years
    @Boldewyn Ur solution doesnot work. Neither for ints nor for floats...i tried to used it but to no avail
  • Boldewyn
    Boldewyn almost 11 years
    @S.Ali Thanks. There's a ? missing: r"^/item/value/(\d+(?:\.\d+)?)$" - for floats it should've worked before, however.
  • Nick T
    Nick T almost 10 years
    Please do not use a bare except: as this will eat every single error, often hiding bugs. You likely want to just catch ValueError's (except ValueError:)
  • Werner Raath
    Werner Raath about 4 years
    Big fan of this one. How can one make r"^item/value/(?P<dollar>\d+\.\d+)$" allow for a negative value?