简体   繁体   English

如何在Django URL模式中使用十进制数?

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

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). 我想在Django URL模式中使用带小数点的数字,但我不确定它是否真的可行(我不是正则表达式专家)。

Here's what I want to use for URLs: 这是我想要用于URL的内容:

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

Those URLs would show items valued at $0.01 or $0.05. 这些网址会显示价值0.01美元或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). 当然,我可以采取简单的方法,并以美分传递值,因此它将是/ item / value / 1,但我希望在我的视图中接收作为十进制数据类型而不是整数的参数(和我可能不得不在某个时候处理一小部分分数)。 Is it possible to write a regex in a Django URL pattern that will handle this? 是否可以在Django URL模式中编写一个可以处理此问题的正则表达式?

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. url不应该以斜杠开头。

in views you can have function: 在视图中你可以有功能:

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

我不知道Django具体,但这应该匹配URL:

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

如果要接受的值仅为0.01美元或0.05美元,则可以像下面这样指定harto的模式:

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

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. 它只会匹配URL模式,比如/item/value/0.01/item/value/12.2等。

It won't match URL patterns like /item/value/1.223 , /item/value/1.2679 etc. 它不会匹配URL模式,比如/item/value/1.223/item/value/1.2679等。

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. 它将匹配URL模式,比如/item/value/0.01/item/value/1.22/item/value/10.223/item/value/1.3等。

Finally you can design your views.py something like 最后你可以设计你的views.py东西

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", {});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Django中通过URL模式重定向? - How do I redirect by URL pattern in Django? 如何将django.core.urlresolvers.reverse与函数引用而不是命名URL模式一起使用? - How do I use django.core.urlresolvers.reverse with a function reference instead of a named URL pattern? 如何使用字符串格式来接收小数点后正好有 5 个数字的数字? - How do I use string formatting in order to receive a number with exactly 5 number after decimal point? 如何在 Django 中正确使用 UUID id 作为 url 参数? - How do I properly use a UUID id as a url parameter in Django? 如何在Django中创建类似controller / action / id的url模式? - How do I create a url pattern like controller/action/id in django? 如何覆盖此模式? django - How do I override this pattern? django 基于模型名称的 Django 动态 url 模式:怎么做? - Django dynamic url pattern based on model names: how to do it? 如何在python中将字符串转换为唯一的十进制数字? - How do I convert a string into a unique decimal number in python? 如何在python中向float(十进制)数字添加一个短语? - how do i add a phrase to a float (decimal) number in python? 如何将字符串转换为十进制数以在 Python 中进行算术运算? - How do I convert a string into a decimal number for arithmetic manipulation in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM