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

前端 未结 4 1579
终归单人心
终归单人心 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:35

    Don't use »

    url(r"^item/value/(?P\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\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", {});
    

提交回复
热议问题