2

In django based project I have a view with a custom decorator:

@login_required
@user_is_project_maintainer
def edit(request, project_key):
   ... 

and actual custom decorator itself:

def user_is_project_maintainer(request):
    def decorator(view_func, *args, **kwargs):
        @wraps(view_func, assigned=available_attrs(view_func))
        def _wrapped_view(request, project_key, *args, **kwargs):
            project = get_object_or_404(Project, key=project_key)

            if (project.maintainer_id != request.user.id) :
                return HttpResponseRedirect(reverse('kifos.views.index', args=(project.key,)))
            else :
                view_func(request, *args, **kwargs)

        return _wrapped_view
    return decorator

And this results in an exception 'function' object has no attribute 'get' deep inside django itself (in /django/core/handlers/base.py line 188). Can't figure out why this happens. Any ideas?

1
  • Did you try looking further up the traceback? Commented Oct 11, 2012 at 13:45

1 Answer 1

4

That looks like an overcomplicated (possibly broken) implementation of a decorator... how about this instead:

def user_is_project_maintainer(view):
    @wraps(view)
    def _wrapped_view(request, project_key, *args, **kwargs):
        project = get_object_or_404(Project, key=project_key)
        if (project.maintainer_id != request.user.id) :
            return HttpResponseRedirect(reverse('kifos.views.index', args=(project.key,)))
        else :
            view(request, project, *args, **kwargs)
    return _wrapped_view

Note that your "edit" view now automatically has the project as its second argument, so you can change it to:

def edit(request, project):
    #can do stuff with project object in here
Sign up to request clarification or add additional context in comments.

1 Comment

Problems with project_key argument - nobody passes

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.