這個正則表達式在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而且我已經進入了高級網址,在這里有一個正則表達式,它沒有解釋:

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 \ w +) - (?P \ w +))/'其中:

  • ?P<> : defines that we are defining a variable
  • ?P <>:定義我們定義一個變量

  • <text> : text is your variable's name
  • :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/
  • 而且 - 介於兩者之間只是一個強制性文本,可以用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 \ w +) - (?P \ w +)/后跟后綴都是由上述視圖管理的。

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


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/01/09/f181c2aab3e834f60e91442b8a8226a8.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com