Guest User

Untitled

a guest
Jul 12th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. Common Regular Expressions for Django URLs
  2. ======
  3.  
  4. A list of common regular expressions for use in django url's regex.
  5.  
  6.  
  7. Example Django URLs patterns:
  8.  
  9. ```python
  10. from django.conf.urls import url, include
  11. # Django 1.9 and Up (required in Django 1.10+)
  12. # urls.py
  13.  
  14. from django.conf.urls import url, include
  15. from appname.views import (
  16. AboutView,
  17. article_detail,
  18. ContactView,
  19. home_view,
  20. profile_detail,
  21. )
  22.  
  23.  
  24. urlpatterns = [
  25. # Examples:
  26. url(r'^$', home_view, name='home'),
  27. url(r'^contact/$', ContactView.as_view(), name='contact'),
  28. url(r'^about/$', AboutView.as_view(), name='about'),
  29. url(r'^profile/(?P<username>[\w.@+-]+)/$', profile_detail, name='profile'),
  30. url(r'^article/(?P<slug>[\w-]+)/$', article_detail, name='article'),
  31. url(r'^blog/', include("blog.urls")),
  32. url(r'^admin/', admin.site.urls),
  33. ]
  34.  
  35.  
  36. # Django 1.8 and below
  37. # urls.py
  38. from appname.views import (
  39. AboutView,
  40. ContactView,
  41. )
  42.  
  43.  
  44. urlpatterns = patterns('',
  45. url(r'^$', 'appname.views.home_view', name='home'),
  46. url(r'^contact/$', ContactView.as_view(), name='contact'),
  47. url(r'^about/$', AboutView.as_view(), name='about'),
  48. url(r'^profile/(?P<username>[\w.@+-]+)/$', 'appname.views.profile_detail', name='about'),
  49. url(r'^article/(?P<slug>[\w-]+)/$', 'appname.views.article_detail', name='about'),
  50. url(r'^blog/', include('blog.urls')),
  51. url(r'^admin/', include(admin.site.urls)),
  52. )
  53.  
  54. ```
  55.  
  56.  
  57.  
  58.  
  59. ### Username (user's username)
  60.  
  61. Regex:
  62.  
  63. ```
  64. (?P<username>[\w.@+-]+)
  65. ```
  66.  
  67. ##### Example:
  68.  
  69. Parameters:
  70.  
  71. ```python
  72. username = '[email protected]'
  73. or
  74. username = 'myusername' ## this paramater can be either email or username fields
  75. ```
  76.  
  77. Query:
  78.  
  79. ```python
  80. object = UserModel.objects.get(username=username)
  81. ```
  82.  
  83. Url:
  84.  
  85. ```
  86. url(?P<username>[\w.@+-]+)$', 'appname.views.show_user'),
  87. ```
  88.  
  89. View:
  90.  
  91. ```python
  92. def show_user(request, username):
  93. ...
  94. return ...
  95. ```
  96.  
  97. Live usage:
  98.  
  99. ```
  100. yourdomain.com/[email protected]/
  101.  
  102. or
  103.  
  104. yourdomain.com/myusername/
  105.  
  106. ```
  107.  
  108.  
  109. ### Object ID (user id, profile id, group id, etc)
  110. Regex:
  111.  
  112. ```
  113. (?P<order>\d+)
  114. ```
  115.  
  116. ##### Example
  117.  
  118. Parameters:
  119.  
  120. ```python
  121. id = 1
  122. ```
  123.  
  124. Query:
  125.  
  126. ```python
  127. object = Model.objects.get(id=id)
  128. ```
  129.  
  130. Url:
  131.  
  132. ```
  133. url(r'^(?P<id>\d+)$', 'appname.views.item_id'),
  134. ```
  135.  
  136. View:
  137.  
  138. ```python
  139. def item_id(request,id):
  140. ...
  141. return ...
  142. ```
  143.  
  144. Live usage:
  145.  
  146. ```
  147. yourdomain.com/12/
  148. ```
  149.  
  150.  
  151. ### Username with Object Order
  152. Regex:
  153. ```
  154. (?P<username>[\w.@+-]+)/(?P<order>\d+)
  155. ```
  156.  
  157. ##### Example
  158.  
  159. Parameters:
  160. ```python
  161. username = "myusername"
  162.  
  163. order = 13
  164. ```
  165.  
  166. Query:
  167.  
  168. ```python
  169. user_object = UserModel.objects.get(username=username)
  170. queryset = UserItem.objects.filter(order=order, user=user)
  171. ```
  172.  
  173. Url:
  174.  
  175. ```
  176. url(r'^(?P<username>[\w.@+-]+)/(?P<order>\d+)/$', 'appname.views.item_home', name='home'),
  177. ```
  178.  
  179. View:
  180.  
  181. ```python
  182. def item_home(request, username, order):
  183. ...
  184. return ...
  185. ```
  186.  
  187. Live usage:
  188. ```
  189. yourdomain.com/useritem/myusername/13/
  190. ```
  191.  
  192.  
  193.  
  194. ### Slugs
  195. Regex:
  196. ```
  197. (?P<slug>[\w-]+)
  198. ```
  199.  
  200. ##### Example
  201.  
  202. Parameters:
  203. ```python
  204. slug = "slugged-item"
  205. ```
  206.  
  207. Query:
  208. ```python
  209. object = Articles.objects.get(slug=slug)
  210. ```
  211.  
  212. Url:
  213. ```
  214. url(r'^(?P<slug>[\w-]+)/$', 'appname.views.article'),
  215. ```
  216.  
  217. View:
  218. ```python
  219. def article(request,article):
  220. ...
  221. return ...
  222. ```
  223.  
  224. Live usage:
  225. ```
  226. yourdomain.com/your-slug/
  227. ````
  228.  
  229.  
  230. ### Digits and Dates (through digits)
  231.  
  232. Regex:
  233.  
  234. ```
  235. 4 Digits like 2015
  236. (?P<year>\d{4})
  237.  
  238. 2 Digits like 12
  239. (?P<month>\d{2})
  240.  
  241. Any Digits like 1231 or 123438192
  242. (?P<article_id>\d+)
  243. ```
  244.  
  245. ##### Example
  246.  
  247. Parameters:
  248. ```python
  249. year = 2015
  250. month = 01
  251. article_id = 412
  252. ```
  253.  
  254. Query:
  255. ```python
  256. year_queryset = Articles.objects.filter(year=year)
  257. months_in_year_queryset = Articles.objects.filter(year=year).filter(month=month)
  258. article_object = Articles.objects.get(id=article_id)
  259. ```
  260.  
  261. Url:
  262. ```
  263. (r'^articles/(?P<year>\d{4})/$', views.year_archive),
  264. (r'^articles/(\d{4})/(?P<month>\d{2})/$', views.month_archive),
  265. (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<article_id>\d+)/$', views.article_detail),
  266. ```
  267.  
  268. View:
  269. ```python
  270. def year_archive(request, year):
  271. return ..
  272.  
  273. def month_archive(request, month):
  274. return ..
  275.  
  276. def article_detail(request, year, month, article_id):
  277. ...
  278. return ...
  279. ```
  280.  
  281. Live usage:
  282.  
  283. ```
  284. yourdomain.com/2015/
  285. yourdomain.com/2015/03/
  286. yourdomain.com/2015/03/21/
  287. ```
  288.  
  289.  
  290.  
  291.  
  292. If you find other regular expressions you are unsure of their meaning, feel free to [contact us](mailto:[email protected]).
  293.  
  294. Thank you!
  295.  
  296. Coding for Entrepreneurs
Add Comment
Please, Sign In to add comment