简体   繁体   English

这个正则表达式在django中意味着什么?

[英]What does this regex mean in django?

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,并且已经进入了高级url,在这里有一个正则表达式,它没有得到解释:

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_slugpage_id以及它们之间的-是什么?

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 eg In case of a blog, urls might read as 每当我们处理可变的url模式时,就使用正则表达式模式,例如在博客的情况下,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. 我们可以在此处看到一个常见的模式,该模式可以与页面slug(或前缀)和页面/帖子ID相关。 So we create two variables namely page_slug and page_id. 因此,我们创建两个变量,即page_slug和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: (注意:可以像其他地方一样将变量名重命名为您喜欢的名称。因此,将正则表达式创建为/(?P<page_slug>\\w+)-(?P<page_id>\\w+))/' ,其中:

  • ?P<> : defines that we are defining a variable ?P<> :定义我们正在定义一个变量
  • < text > : text is your variable's name < text >:text是您变量的名称
  • \\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. \\w+ :是您的正则表达式,用于定义可接受的模式(在这种情况下,\\ w表示集合[0-9a-zA-Z_]任何内容,而+表示任意数量的重新排列。如果您想了解更多有关此信息的信息,请参考https://www.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html以获取参考,并请http://regexr.com/获得有关实践。
  • and the - in between is simply a compulsary text which could have been replaced with say -no- to look like domain.com/page-no-1/ -之间仅仅是这可能被替换为文本必须放说-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. 其余的标记与普通url相似,这意味着以给定模式(?P<page_slug>\\w+)-(?P<page_id>\\w+)/开头的所有url都由上述视图处理。

eg - domain.com/post-1/history/ - is handeled by views.history and so on. 例如- domain.com/post-1/history/ -由handeled 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.argsself.kwargs因此您可以通过self.kwargs['page_slug']来访问它

This regex matches the following urls: 此正则表达式与以下网址匹配:

/abc-def/history/ (abc goes to page_slug and def to page_id)
/ghi-jkl/edit/

etc 等等

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM