django_url_cheat.

txt Page 1 of 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

Aug/2008

from django.conf.urls.defaults import * urlpatterns = patterns('', # (regular exp, function, optional dictionary, optional name) # Doesn’t differentiate request method, all go to the same function (r'^articles/2003/$', 'news.views.special_case_2003'), # "/articles/2003" -> no match need "2003/" (r'^articles/(\d{4})/$', 'news.views.year_archive'), # ordering matters # "/articles/2003/" -> news.views.special_case_2003, not news.views.year_archive(2003) (r'^articles/special/(?P<year>\d{4})/$', 'news.views.year_archive'), # "/articles/special/2003" -> news.views.year_archive(request, year='2003') (r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'), (r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'), # "/articles/2003/03/3/" -> news.views.article_detail(request, '2003', '03', '3') ) urlpatterns += patterns('sports.views', # append like a list (r'^sports/2003/$', 'some_function'), # "/sports/2003/" -> sports.views.some_function(request) ) # Generic actions are useful if you are doing something generic such as: # by default {'extra_context':{}}, add more context into extras if necessary urlpatterns += patterns('django.views.generic.simple', (r'^page_new/(?P<id>\d+)/$', 'direct_to_template', {'template': 'page_detail.html'}),) urlpatterns += patterns('django.views.generic.simple', (r'^page/(?P<id>\d+)/$', 'redirect_to', {'url': '/page_new/%(id)s/'}},) urlpatterns += patterns('django.views.generic.list_detail', (r'^page/all/$', 'object_list', {'queryset': Pages.objects.all() }),) # default: {'paginate_by':'infinity' , 'page':'1', # 'template_name':'app/model_list.html' } urlpatterns += patterns('django.views.generic.list_detail', (r'^page/all/(?P<id>\d+)/$', 'object_detail', {'queryset': Pages.objects.all(), 'object _id':id }),) # default: {'paginate_by':'infinity' , 'page':'1', # 'template_name':'app/model_detail.html' } urlpatterns += patterns('django.views.generic.create_update', (r'^...$', 'create_object', {'model':SomeModel or 'form_class':SomeForm }),) # default: {'post_save_redirect':object.get_absolute_url(), 'login_required':False, # 'template_name':'app/model_form.html' } urlpatterns += patterns('django.views.generic.create_update', (r'^...$', 'update_object', {'model': / 'form_class':, 'object_id':SomeID }),) # default: {'post_save_redirect':object.get_absolute_url(), 'login_required':False, # 'template_name':'app/model_form.html' } urlpatterns += patterns('django.views.generic.create_update', (r'^...$', 'delete_object', {'model': / 'form_class':, 'object_id':SomeID }),) # default: {'post_save_redirect':object.get_absolute_url(), 'login_required':False, # 'template_name':'app/model_confirm_delete.html' }

# Parents are good for subdividing the work urlpatterns += patterns('', # use include to add child url matchers: (r'^weblog/(?P<idName>\w+)/', include('mysite.app.url')), ) # in file app/url.py: from django.conf.urls.defaults import * urlpatterns = patterns('app.views', (r'^$', 'blog.index'), # "/weblog/me/" -> app.views.blog.index(request, idName='me') (r'^post/(?P<postIndex>\d+)$', 'post.show'), # "/weblog/me/12" -> app.views.post.show(request, idName='me', postIndex='12') (r'^details/$', 'blog.details', {'extraData', 'foo!'}) # "/weblog/details/" -> app.views.blog.details(request, idName='me', extraData='foo!') (r'^post/(?P<pid>\d+)/comment/(?P<cid>\d+)/$', 'post.show', {'gotoComment', 'true'}, "w eblog-viewComment"), 62 # "/weblog/post/1/comment/1/" -> app.views.blog.details(request, idName='me', pid='1', cid='1', gotoComment='true')
- 1 -

django_url_cheat.txt Page 2 of 2 63

Aug/2008

# the template tag {% url weblog-viewComment pid=1,cid=1 %} returns "/weblog/post/1/com ment/1/" 64 ) 65 66 # often you will write one function which has a default parameter to save code: 67 urlpatterns = patterns('app.views', 68 (r'^$', 'blog.index'), 69 (r'^/(?P<postIndex>\d+)/$', 'blog.index')) 70 def index(request, postIndex='1') 71 .... 72 73 # often we want to find a url that will execute a function with some parameters 74 # we would use {% url function args %} in a template. in code we would use: 75 from django.core.urlresolvers import reverse 76 reverse(viewname, urlconf=None, args=None, kwargs=None) 77 def myview(request): 78 return HttpResponseRedirect(reverse('weblog-viewComment', args='pid=1,cid=1')) 79 80 # regular reference: 81 # . any char 82 # ^ start of string $ end of string 83 # * 0 or more of preceding + 1 or more of preceding 84 # ? 0 or 1 of preceding (?!..) matches when it doesnt match .. 85 # *? 0 or more, minimal match +? 1 or more, minimal match 86 # {m} exactly m of preceding {m,n} between m to n of preceding 87 # [..] eg. [abc],[a-z],[0-9a-z] [^..] matches if doesn't match [..] 88 # (..) groups what's inside (?=..) matches .. but doesn't consume it 89 # \d [0-9] (decimal digit) \D [^0-9] (non-digit) 90 # \w [a-zA-Z0-9_] (alphanumeric) \W [^a-zA-Z0-9_] (non-alphanumeric) 91 # \s [ \t\n\r\f\v] (whitespace) \S [^ \t\n\r\f\v] (non-whitespace) 92 93 # Request and Response Object 94 def index(request, index='1') 95 request.path # /weblog/me/ 96 request.method # either 'GET', 'POST', 'HEAD', ... 97 request.GET['someVarName'] # whatever it should be 98 request.GET['someVarName', 'default'] # if it doesn't exist then default. also for POST 99 request.POST['someVarName'] 100 request.REQUEST['someName'] # searches GET then FILES 101 request.COOKIES['attributeName'] 102 request.FILES['someFilename'] # request.POST does not have files 103 # includes methods: read(num_bytes=...), chunk() and attrs: file_name, file_size 104 request.META['someMetaName'] 105 # includes: CONTENT_LENGTH, CONTENT_TYPE, HTTP_ACCEPT_ENCODING, SERVER_PORT, 106 # HTTP_ACCEPT_LANGUAGE, HTTP_HOST, HTTP_REFERER, HTTP_USER_AGENT, 107 # QUERY_STRING, REMOTE_ADDR, REMOTE_HOST, REQUEST_METHOD, SERVER_NAME, 108 request.user # object: django.contrib.auth.models.User 109 request.get_full_path() # includes any stuff after the last directory / 110 request.build_absolute_uri() # includes a http://www.. bit that is read from their side 111 request.is_ajax() # major ajax libraries send a signal that a query is for ajax 112

- 2 -