On 3 déc, 08:37, Todd Blanchard <tblanch...@mac.com> wrote:
(snip)
>


A couple observations if you don't mind:

> urlpatterns += patterns('',
>     
> (r'^(?P<app>[^/]+)/(?P<view>[^/]+)/(?P<id>[^/]+)\.(?P<format>[^/]+)',dispatch_request),
>     (r'^(?P<app>[^/]+)/(?P<view>[^/]+)/(?P<id>[^/]+)',dispatch_request),
>     (r'^(?P<app>[^/]+)/(?P<view>[^/]+)\.(?P<format>[^/]+)',dispatch_request),
>     (r'^(?P<app>[^/]+)/(?P<view>[^/]+)',dispatch_request),
>     (r'^(?P<app>[^/]+)\.(?P<format>[^/]+)',dispatch_request),
>     (r'^(?P<app>[^/]+)',dispatch_request),
> )
>
> def dispatch_request(request, app, view='index', id=None, format='html'):
>     """
>     Implements Ruby on Rails 'Convention over Configuration' Policy
>
>     Along with rules in urls.py, implements urls of type
>     /application/view/id.format
>     which is analogous to Rails's
>     /controller/action/id.format
>     """
>
>     view_module = None
>     view_function = None
>
>     # if debug, let the exceptions fly - otherwise make them 404's
>     # try to import the application module, then lookup the view function in 
> it
>     if settings.DEBUG:
>         view_module = __import__(app+'.views', globals(), locals(), [ view ], 
> -1)
>         view_function = (view_module.__getattribute__(view))

this is usually spelled:

         view_function = getattr(view_module, view)


As a general rule, __magic_names__ are implementation support for
operators or operator-loke generic functions, and it's considered bad
style to directly access the implementation without a good reason.

>     else:
>         try:
>             view_module = __import__(app+'.views', globals(), locals(), [ 
> view ], -1)
>             view_function = (view_module.__getattribute__(view))
>         except ImportError:

And what if view_module doesn't define a 'view' function ?
You want to check for AttributeError too

>             raise Http404

This is a somehow complicated and not really dry way to write a simple
thing;

        try:
            view_module = __import__(app+'.views', globals(), locals
(), [ view ], -1)
            view_function = (view_module.__getattribute__(view))
        except (ImportError, AttributeError):
            if settings.DEBUG:
                 raise # reraise the exception
            else:
                  raise Http404


>     # make the GET mutable and stick format and id on it if non-nil
>     if request.GET:
>         request.GET = request.GET.copy()
>     else:
>         request.GET = QueryDict('').copy()
>
>     request.GET.__setitem__('format',format)

Same remark as above. This is usually spelled:

      request.GET['format'] = format

(snip)

>
> and this gives me most of the convention of rails.  No more fiddling urls.py.

Ahem... Django's ecosystem is a lot about reusable "pluggable" apps.
Most of them provide default more or less generic views, but sometimes
- more often for non-trivial projects - they don't really fit your
current project needs, so you provide your own views instead. Your
above scheme may be a problem in this case.

Also, all callables in a view module are not views. Remember that
classes are callable too, that any name imported in a module are
impotable from this module (if module_b imports class A from module_a,
you can access A from module_b), and that most often than not a view
module starts with quite a few import statements...


> The only improvement would be to make the functions in views.py methods on an 
> object (the "controller") and bind its ivars into the request when rendering 
> the template.  

I really think you're trying too hard to force Rails idioms into
Django - kind of a "I can write C in any language" syndrom. Fighting
against the tool is never a good idea. If you really want routes and
rails-like controllers, you'd be way happier with Pylons.

My 2 cents, really.

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


Reply via email to