router view

 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
## exampleapp/views.py
import re
from django.http import Http404, HttpResponse
from django.utils.datastructures import SortedDict

class RouterView(object):
    def __init__(self):
        self.mapping = SortedDict()

    def register(self, *args):
        for regex, view_func in args:
            self.mapping[re.compile(regex)] = view_func

    def __call__(self, request, *args, **kwargs):
        for regex, view_func in self.mapping.items():
            if regex.match(request.path[1:]):
                return view_func(request, *args, **kwargs)
        # does not match
        raise Http404

def some_view(request):
    return HttpResponse('test')

## urls.py
from django.conf.urls.defaults import *
from exampleapp.views import RouterView, some_view

router = RouterView()
router.register(
    (r'^foo/', some_view),
)

urlpatterns = patterns('',
    (r'', router),
    (r'foo/bar/', router),
)

More like this

  1. view ModelForm by hrdcdr 1 year ago
  2. Register django views by magik_cypress 2 years, 2 months ago
  3. Generic Views for newforms by jnievas 6 years, 2 months ago
  4. simple_view by troolee 3 years, 9 months ago
  5. clear cache view by zodman 1 year, 4 months ago

Comments

diegueus (on January 20, 2010):

This is cool, but for a lot of web developers is a bad practice, IMHO i think is usefull for small websites

#

(Forgotten your password?)