How to use return redirect as a callback function in Django

564 views Asked by At

I have been trying to get my code working through out the whole day. I have a view function but its too verbose plus i am trying to use its implementation on another view so i am trying to refactor the code for the function to be able to run in another view. Have been able to get the function to work except for the fact that there is a return redirect with the profile name within the function(its just a plain function and not a view function). Everytime i try to run the code by passing the redirect as a call back function it always produces an HttpRedirectError and also displays the location of the object in memory, how can i go about using the return redirect as a callback or whichever way i can get it working with my code. The Function code:

def foo(filter,images,request,redirector):
 #do something
 return redirect(redirector)

The View Code:

if something:  
  foo(filter,images,request,'home')

For the View have also tried:

def redirector():
  return 'home'
foo(property,images,request,redirector)

Is there any way i can get this to work, if i don't get it to work, would have to repeat unecessary codes for another function.

1

There are 1 answers

0
Gin Fuyou On

Before suggesting a solution I must say that it seems you are lacking in understanding of basic python here and inventing some strange approaches to building Django views. You need to review how functions operate, what return statement means and so on.

For a start Django view is just a function that takes request object and returns a response object. Having a helper function that runs within it is totally fine, you could place it in helpers.py or any file that makes sense to you. But why you make it return a redirect? You seem to just complicate your own task.

Let's have a very simple example. Let's say you want view to find user's favourite picture from own gallery, using a helper:


# picture model
class Picture(models.Model):
    # correct way to refer user model for your project
    owner = models.ForeignKey(settings.AUTH_USER_MODEL)  
    rating = models.PositiveSmallIntegerField(default=5)
    # image file field
    img = models.ImageField()  

# helper function - find a picture with highest rating from a queryset
def get_favourite_picture(picture_qs):
    # order queryset by rating descending
    picture_qs = picture_qs.oreder_by('-rating') 
    # select first item 
    picture = picture_qs.first()  
    # return from function only the picture object as result
    return picture  

# view
def redirect_to_favourite(request):
    # it's how you get pictures where owner is current user
    picture_qs = request.user.picture_set.all()
    # let function handle just getting a pic
    picture = get_favourite_picture(picture_qs)
    # and let the view handle the redirect (return redirect response)
    # to the view that shows a single image by it's PK (Primary Key - an id)
    return redirect("picture_detail", kwargs={'pk': picture.pk})