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

发布于 2024-07-26 12:15:46 字数 322 浏览 4 评论 0原文

我想在 Django URL 模式中使用带小数点的数字,但我不确定这是否真的可行(我不是正则表达式专家)。

以下是我想要使用的 URL:

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

这些 URL 将显示价值为 0.01 美元或 0.05 美元的商品。 当然,我可以采取简单的方法并以美分为单位传递值,因此它将是 /item/value/1,但我希望在我看来以十进制数据类型而不是整数接收参数(并且在某些时候我可能不得不处理一小部分)。 是否可以在 Django URL 模式中编写一个正则表达式来处理这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乙白 2024-08-02 12:15:46

可能是

urlpatterns = patterns('',
   (r'^item/value/(?P<value>\d+\.\d{2})/

url 不应该以斜杠开头。

在视图中你可以有功能:

def byvalue(request,value='0.99'):
    try:
        value = float(value)
    except:
        ...
, 'myapp.views.byvalue'), ... more urls )

url 不应该以斜杠开头。

在视图中你可以有功能:

It can be something like

urlpatterns = patterns('',
   (r'^item/value/(?P<value>\d+\.\d{2})/

url should not start with slash.

in views you can have function:

def byvalue(request,value='0.99'):
    try:
        value = float(value)
    except:
        ...
, 'myapp.views.byvalue'), ... more urls )

url should not start with slash.

in views you can have function:

驱逐舰岛风号 2024-08-02 12:15:46

我具体不了解 Django,但这应该与 URL 匹配:

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

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

r"^/item/value/(\d+\.\d+)$"
轮廓§ 2024-08-02 12:15:46

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

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

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})$"
梦幻的心爱 2024-08-02 12:15:46

不要使用 »

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

它只会匹配 URL 模式,例如 /item/value/0.01/item/value/12.2 等。

它不会匹配 URL 模式,例如 /item/value/1.223/item/value/1.2679 等。

最好使用 »

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

它将匹配URL模式,例如/item/value/0.01/item/value/1.22/item/ value/10.223/item/value/1.3

最后你可以设计你的 views.py 类似的东西

这只是一个例子。

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

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", {});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文