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.
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
returnstatement 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.pyor 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: