Django with a custom view decorator causing "Page not redirecting properly" -


i'm trying write custom decorator class-based view in django. want check particular condition, user listed owner of scene object. have decorator written, , works fine if user owner - allows user continue page properly. if user not owner, "this page isn't redirecting properly" error.

here decorator:

def scene_is_yours(function):     def wrap(request, *args, **kwargs):         pk = kwargs.get('pk')         scene = scene.objects.get(id=pk)         if not (scene.user.id == request.user.id):             return httpresponseredirect('project-mine')          return function(request, *args, **kwargs)     return wrap 

here view:

class sceneupdateview(templateview):     model = scene     fields = ['name', 'short_name', 'description']     template_name = 'scene/edit.html'      @method_decorator(login_required)     @method_decorator(scene_is_yours)     def dispatch(self, *args, **kwargs):         return super(sceneupdateview, self).dispatch(*args, **kwargs) 

my traceback these 2 lines repeatedly:

[07/jul/2015 22:10:35]"get /accounts/login/?next=/scenes/1/edit/ http/1.1" 302 0 [07/jul/2015 22:10:35]"get /scenes/1/edit/ http/1.1" 302 0 

i've tried removing login_required decorator, had no effect. user account i'm testing logged in, can't figure out why it's trying send me login , decorated view. going on?

try wraps decorator functools , maybe u shou rename function variable, , maybe shoud use django urlresolvers:

from functools import wraps django.core.urlresolvers import reverse ...     def scene_is_yours(view_function):          @wraps(view_function)         def wrap(request, *args, **kwargs):             pk = kwargs.get('pk')             scene = scene.objects.get(id=pk)             if not (scene.user.id == request.user.id):                 return httpresponseredirect(reverse('project-mine'))              return view_function(request, *args, **kwargs)         return wrap 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -