Guest User

Untitled

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