I'm learning django from a book and I've got into advanced urls, in here there is a regex that it's not explained:
我正在从一本书中学习django而且我已经进入了高级网址,在这里有一个正则表达式,它没有解释:
urlpatterns = [
url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/',
include([
url(r'^history/$', views.history),
url(r'^edit/$', views.edit),
url(r'^discuss/$', views.discuss),
url(r'^permissions/$', views.permissions),
])),
]
I understand that it's about removing redundancy, but how does it actually work? Where do you get page_slug
and page_id
from and what's with the - between them?
我知道这是关于删除冗余,但它是如何实际工作的?你从哪里得到page_slug和page_id以及它们之间有什么关系?
2 个解决方案
#1
2
If you are moving onto advanced urls I presume you understand how the basic url markup works. The regex patterns are used whenever we are dealing with variable url patterns for e.g. In case of a blog, urls might read as
如果您要转到高级网址,我认为您了解基本网址标记的工作原理。每当我们处理可变url模式时,就会使用正则表达式模式。如果是博客,网址可能会显示为
domain.com/post-1/
domain.com/post-2/
or
domain.com/shortpost-1/
domain.com/shortpost-2/
and so on.
等等。
We can see a common pattern here which can be related to as a page slug( or prefix) and page/post id. So we create two variables namely page_slug and page_id. (Note: The variable names like anywhere else can be renamed to your liking. The regex is hence created as /(?P<page_slug>\w+)-(?P<page_id>\w+))/'
where:
我们可以在这里看到一个常见的模式,它可以作为页面slug(或前缀)和page / post id相关。所以我们创建了两个变量,即page_slug和page_id。 (注意:像其他地方一样的变量名称可以根据自己的喜好重命名。因此正则表达式创建为/(?P
-
?P<>
: defines that we are defining a variable - <
text
> : text is your variable's name -
\w+
: is your regex which defines what pattern is acceptable (In this case \w represents anything in the set[0-9a-zA-Z_]
and+
represents any number of repititions . If you want to learn more on this I'll refer https://www.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html for reference and http://regexr.com/ for practise. - and the
-
in between is simply a compulsary text which could have been replaced with say-no-
to look likedomain.com/page-no-1/
?P <>:定义我们定义一个变量
\ w +:是你的正则表达式,它定义了什么模式是可接受的(在这种情况下\ w表示集合中的任何内容[0-9a-zA-Z_],+表示任意数量的重复。如果你想了解更多关于这个我'请参考https://www.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html以供参考,http://regexr.com/进行练习。
而且 - 介于两者之间只是一个强制性文本,可以用say -no-代替,看起来像domain.com/page-no-1/
The rest of the markup is similar to normal urls which means that any url begining in the given pattern (?P<page_slug>\w+)-(?P<page_id>\w+)/
followed by the suffix is handeled by the mentioned view.
标记的其余部分类似于普通网址,这意味着任何以给定模式开头的网址(?P
e.g. - domain.com/post-1/history/
- is handeled by views.history
and so on.
例如 - domain.com/post-1/history/ - 由views.history等提供。
The important part now is how do these variable names affect your views. If you are using function based views, your history view will be defined as :
现在重要的部分是这些变量名称如何影响您的视图。如果您使用的是基于功能的视图,则您的历史记录视图将定义为:
def history(request, page_slug, page_id):
#Your code using the two variables received.
#These might be values stored in db to dynamically fetch values
In class based views to access the url parameters you use self.args
or self.kwargs
so you would access it by doing self.kwargs['page_slug']
在基于类的视图中访问url参数,你使用self.args或self.kwargs,这样你就可以通过执行self.kwargs ['page_slug']来访问它
#2
0
This regex matches the following urls:
此正则表达式匹配以下URL:
/abc-def/history/ (abc goes to page_slug and def to page_id)
/ghi-jkl/edit/
etc
#1
2
If you are moving onto advanced urls I presume you understand how the basic url markup works. The regex patterns are used whenever we are dealing with variable url patterns for e.g. In case of a blog, urls might read as
如果您要转到高级网址,我认为您了解基本网址标记的工作原理。每当我们处理可变url模式时,就会使用正则表达式模式。如果是博客,网址可能会显示为
domain.com/post-1/
domain.com/post-2/
or
domain.com/shortpost-1/
domain.com/shortpost-2/
and so on.
等等。
We can see a common pattern here which can be related to as a page slug( or prefix) and page/post id. So we create two variables namely page_slug and page_id. (Note: The variable names like anywhere else can be renamed to your liking. The regex is hence created as /(?P<page_slug>\w+)-(?P<page_id>\w+))/'
where:
我们可以在这里看到一个常见的模式,它可以作为页面slug(或前缀)和page / post id相关。所以我们创建了两个变量,即page_slug和page_id。 (注意:像其他地方一样的变量名称可以根据自己的喜好重命名。因此正则表达式创建为/(?P
-
?P<>
: defines that we are defining a variable - <
text
> : text is your variable's name -
\w+
: is your regex which defines what pattern is acceptable (In this case \w represents anything in the set[0-9a-zA-Z_]
and+
represents any number of repititions . If you want to learn more on this I'll refer https://www.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html for reference and http://regexr.com/ for practise. - and the
-
in between is simply a compulsary text which could have been replaced with say-no-
to look likedomain.com/page-no-1/
?P <>:定义我们定义一个变量
\ w +:是你的正则表达式,它定义了什么模式是可接受的(在这种情况下\ w表示集合中的任何内容[0-9a-zA-Z_],+表示任意数量的重复。如果你想了解更多关于这个我'请参考https://www.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html以供参考,http://regexr.com/进行练习。
而且 - 介于两者之间只是一个强制性文本,可以用say -no-代替,看起来像domain.com/page-no-1/
The rest of the markup is similar to normal urls which means that any url begining in the given pattern (?P<page_slug>\w+)-(?P<page_id>\w+)/
followed by the suffix is handeled by the mentioned view.
标记的其余部分类似于普通网址,这意味着任何以给定模式开头的网址(?P
e.g. - domain.com/post-1/history/
- is handeled by views.history
and so on.
例如 - domain.com/post-1/history/ - 由views.history等提供。
The important part now is how do these variable names affect your views. If you are using function based views, your history view will be defined as :
现在重要的部分是这些变量名称如何影响您的视图。如果您使用的是基于功能的视图,则您的历史记录视图将定义为:
def history(request, page_slug, page_id):
#Your code using the two variables received.
#These might be values stored in db to dynamically fetch values
In class based views to access the url parameters you use self.args
or self.kwargs
so you would access it by doing self.kwargs['page_slug']
在基于类的视图中访问url参数,你使用self.args或self.kwargs,这样你就可以通过执行self.kwargs ['page_slug']来访问它
#2
0
This regex matches the following urls:
此正则表达式匹配以下URL:
/abc-def/history/ (abc goes to page_slug and def to page_id)
/ghi-jkl/edit/
etc