4
    from django.contrib.auth.decorators import permission_required  
    from django.utils.decorators import method_decorator  
    class EnvCreate(CreateView):

           model = Capacity.models.Env
           fields = ["name","dns","manager"]
           template_name_suffix = '_create_form'

           @method_decorator(permission_required('Capacity.add_env'))
           def get_success_url(self):
                  return reverse("envapps", kwargs={"envid": self.object.pk})

I want to allow a user to be able to add a 'env' only if he has permission.
I have read many messages and blogs and the method to do the same is as above.
But everywhere dispatch() is being used, but i wanted to use it before get_success_url (not sure if this is the problem)

The error i get when i try to create env (by a user who has permission) i get,

    TypeError at /Capacity/create/
    _wrapped_view() takes at least 1 argument (0 given)
    Request Method: POST
    Request URL:    http://172.16.68.20:7000/Capacity/create/
    Django Version: 1.6.1
    Exception Type: TypeError
    Exception Value: _wrapped_view() takes at least 1 argument (0 given)
    Exception Location: /usr/lib/python2.6/site-packages/django/utils/decorators.py in _wrapper, line 29

Any idea about what am i doing wrong or what can be the solution?

2 Answers 2

7

Got the answer. Was making a mistake in understanding the use of dispatch.

    from django.contrib.auth.decorators import permission_required
    from django.utils.decorators import method_decorator
    class EnvCreate(CreateView):

       model = Capacity.models.Env
       fields = ['name','dns','manager']
       template_name_suffix = '_create_form'

       @method_decorator(permission_required('Capacity.add_env',raise_exception=True))
       def dispatch(self, request):
            return super(EnvCreate, self).dispatch(request)

       def get_success_url(self):
            return reverse('envapps', kwargs={'envid': self.object.pk})
0

Class based views methods cannot be decorated like that: https://docs.djangoproject.com/en/1.6/topics/class-based-views/intro/#decorating-the-class You should actually look into Mixins, this would be the best method to implement such functionality. You can easily see (and maybe even re-use) this kind of functionality by checking Django Braces: https://github.com/brack3t/django-braces

Further more why would you implement this functionality during the get_success_url method? If a user is not permitted to perform the action he should be denied before any data submission, this is safer, has less system load in terms of traffic and operations. If you still need to perform such a task, create a Mixin, check in the get_success_url for permissions, if the user has permission proceed as normal, if not throw and exception, mind though that get_success_url is called after the successful validation of the form, which means that your data is already saved, you should be hacking into the form_valid method for additional checking if needed apart from maybe a logedin or permission check.

1
  • Got the solution. Instead of adding the decorator before get_success_url, i just had to add a dispatch function as indicated in the link given by you above [link] (docs.djangoproject.com/en/1.6/topics/class-based-views/intro/…). So i just left get_success_url alone, as it was before. And added @method_decorator(permission....) before the new dispatch function. Thanks for your help Jan 21, 2014 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.