Login

default url routing and shortcut

Author:
vicalloy
Posted:
June 11, 2009
Language:
Python
Version:
1.0
Score:
-2 (after 4 ratings)

from url_helper import execute, url_

import views

urlpatterns += patterns('',

url(r'^(?P<urls>.*)', execute, {'views': views}),

)

url_(r’/space/:username/:tag/’, views.url_),

equal

url(r’^space/(?P[^/]+)/(?P[^/]+)/$’,

 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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from django import http
from django.conf.urls.defaults import url
import re
 
def execute(request, urls, views):
    """
    urls [methodName/]param1/param2/.../
    methodName default index
    """
    def get_method(views, methodName):
        try:
            return getattr(views, methodName)
        except Exception, e:
            return None
    method = None
    params = [e for e in urls.split("/") if e]
    params.reverse()
    if params:
        method = get_method(views, params.pop())
    if not method:
        method = get_method(views, 'index')
    if not method:
        raise http.Http404('The requested admin page does not exist.')
    return method(request, *params)
 
def url_(*args,**dic):
    regex = args[0]
    if regex[0] == "/":
        regex = regex[1:]
    regex = '^' + regex
    regex = regex + '$'
    regex = re.sub(":[^/]+",
            lambda matchobj: "(?P<%s>[^/]+)" % matchobj.group(0)[1:],
            regex)
    return url(regex, *args[1:], **dic)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

bl4th3rsk1t3 (on June 12, 2009):

what does this do?

#

Please login first before commenting.