#28999: URL Reverse does not work for CBV (Class Based Views)
---------------------------------+--------------------------------------
     Reporter:  airstandley      |                    Owner:  nobody
         Type:  Bug              |                   Status:  new
    Component:  Generic views    |                  Version:  2.0
     Severity:  Normal           |               Resolution:
     Keywords:  url reverse cbv  |             Triage Stage:  Unreviewed
    Has patch:  0                |      Needs documentation:  0
  Needs tests:  0                |  Patch needs improvement:  0
Easy pickings:  0                |                    UI/UX:  0
---------------------------------+--------------------------------------

Comment (by airstandley):

 So I think I have a possible fix, but I'm not yet set up for contributing
 to Django so I haven't run the test suite on it. Preliminary testing
 appeared to work.

 I would be interested in hearing others thoughts before investing more
 time into this.

 {{{

     @classonlymethod
     def as_view(cls, **initkwargs):
         """
         Main entry point for a request-response process.
         """
         for key in initkwargs:
             if key in cls.http_method_names:
                 raise TypeError("You tried to pass in the %s method name
 as a "
                                 "keyword argument to %s(). Don't do that."
                                 % (key, cls.__name__))
             if not hasattr(cls, key):
                 raise TypeError("%s() received an invalid keyword %r.
 as_view "
                                 "only accepts arguments that are already "
                                 "attributes of the class." %
 (cls.__name__, key))

         class callable_view(object):
             # Instance of callable_view is callable and acts as a view
 function
             def __call__(self, request, *args, **kwargs):
                 self = cls(**initkwargs)
                 if hasattr(self, 'get') and not hasattr(self, 'head'):
                     self.head = self.get
                 self.request = request
                 self.args = args
                 self.kwargs = kwargs
                 return self.dispatch(request, *args, **kwargs)
             def __hash__(self):
                 return hash(cls)  # Class' 'view function' hash is defined
 by the Class
         view = callable_view()
         view.view_class = cls
         view.view_initkwargs = initkwargs

         # take name and docstring from class
         update_wrapper(view, cls, updated=())

         # and possible attributes set by decorators
         # like csrf_exempt from dispatch
         update_wrapper(view, cls.dispatch, assigned=())
         return view
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28999#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.64f67f48a53baea8c8c461e3101a6859%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to