Python 整数的Django URL模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23420655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:57:40  来源:igfitidea点击:

Django URL Pattern For Integer

pythonregexdjango-urls

提问by user2114189

I'm new to Python and Django. I added a URLPattern into urls.py as below:

我是 Python 和 Django 的新手。我在 urls.py 中添加了一个 URLPattern,如下所示:

    url(r'^address_edit/(\d)/$', views.address_edit, name = "address_edit"),

I want my url accept a parameter of an integer with variable length, E.g. 0, 100, 1000, 99999, for the "id" of a db table. However I found that I only accept only one digits in above pattern. If i pass a integer not 1 digit only (e.g. 999999), it show an error

我希望我的 url 接受一个长度可变的整数参数,例如 0、100、1000、99999,作为 db 表的“id”。但是我发现我只接受上述模式中的一位数字。如果我传递一个整数而不是 1 位数字(例如 999999),它会显示错误

Reverse for 'address_edit' with arguments '(9999999,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['address_book/address_edit/(\d)/$']

How do I construct the URL Pattern that let the program accept any number of digits integer from URL?

如何构造让程序接受来自 URL 的任意数量的数字整数的 URL 模式?

采纳答案by thefourtheye

The RegEx should have +modifier, like this

RegEx 应该有+修饰符,像这样

^address_edit/(\d+)/$

Quoting from Python's RegEx documentation,

引自Python 的 RegEx 文档

'+'

Causes the resulting RE to match 1 or more repetitions of the preceding RE. ab+will match afollowed by any non-zero number of bs; it will not match just a.

'+'

导致生成的 RE 匹配前面 RE 的 1 次或多次重复。ab+将匹配a后跟任何非零数量的bs;它不会匹配a

\dwill match any numeric digit (0-9). But it will match only once. To match it twice, you can either do \d\d. But as the number of digits to accept grows, you need to increase the number of \ds. But RegEx has a simpler way to do it. If you know the number of digits to accept then you can do

\d将匹配任何数字 ( 0-9)。但它只会匹配一次。要匹配两次,您可以执行\d\d. 但是随着接受位数的增加,您需要增加\ds的数量。但是 RegEx 有一个更简单的方法来做到这一点。如果您知道要接受的位数,那么您可以这样做

\d{3}

This will accept three consecutive numeric digits. What if you want to accept 3 to 5 digits? RegEx has that covered.

这将接受三个连续的数字。如果你想接受 3 到 5 位数字怎么办?RegEx 已经涵盖了这一点。

\d{3,5}

Simple, huh? :) Now, it will accept only 3 to 5 numeric digits (Note: Anything lesser than 3 also will not be matched). Now, you want to make sure that minimum 1 numeric digit, but maximum can be anything. What would you do? Just leave the range open ended, like this

很简单吧?:) 现在,它只接受 3 到 5 个数字(注意:任何小于 3 的数字也不会匹配)。现在,您要确保最小 1 位数字,但最大可以是任何数字。你会怎么办?只是让范围开放结束,就像这样

\d{3,}

Now, RegEx engine will match minimum of 3 digits and maximum can be any number. If you want to match minimum one digit and maximum can be any number, then what would you do

现在,RegEx 引擎将匹配最少 3 位数字,最多可以是任何数字。如果你想匹配最小一位,最大可以是任何数字,那么你会怎么做

\d{1,}

Correct :) Even this will work. But we have a shorthand notation to do the same, +. As mentioned on the documentation, it will match any non-zero number of digits.

正确:) 即使这样也可以。但是我们有一个速记符号来做同样的事情,+. 如文档中所述,它将匹配任何非零位数。

回答by SuperNova

In Django >= 2.0 version, this is done in a more cleaner and simpler way like below.

在 Django >= 2.0 版本中,这是以更简洁、更简单的方式完成的,如下所示。

from django.urls import path

from . import views

urlpatterns = [
    ...
    path('address_edit/<int:id>/', views.address_edit, name = "address_edit"),
    ...
]