tags:

views:

83

answers:

2

Hi,

I have written a function having the following signature:

def action_handler(request, model):

This action_handler is used from different views and handles the actions for this views. One example is deleting objects. In this example the user selectes some objects, selects the delete action and then the user is presented a page to check whether he/she wants to really delete the selected objects. This is done by the following code:

context = {
                    'action_name' : selected_action,
                    'object_list' : object_list,
                }
                return render_to_response("crm/object_delete_check.html", context,
                                       context_instance=RequestContext(request))

For the case that something goes wrong I want to redirect the user to the view from where the user called the action.

Thus I want to ask here whether it is possible to get the calling view from the request object or somewhere else from.

If the def "def action_handler(request, model):" is called from the view "contacts(request):" then i want to redirect the user to the view "contacts(request):" .

But the clue is I do not want to hard-code it since the def action_handler is called from different views. Using a simple "return" is also not possible, since I want to recall the view completely.

A: 

You can pass previous page url through GET parameter:

/object_delete_check/?previous=/contacts/

(see contrib.auth.decorators.login_required for example)

Antony Hatchkins
+1  A: 
if goback: #goback being whatever criteria means "something went wrong"
    default_back_url = "someurl_in_case_the_meta_is_messed_up"
    back = request.META.get('HTTP_REFERER',default_back_url) #yeah they spelled referrer wrong
    if back:
        return HttpResponseRedirect(back)
    else:
        return HttpResponseRedirect(default_back_url)

while META can be faked, it's harder to fake than GET query strings.

Brandon H
I think it's more appropriate to ask in this case: Does it even matter if the URL is faked? As long as the site is secure, I generally don't care about users that mess with requests.One thing that may be a concern is if HTTP_REFERER blocking is common. I don't think it is though.
Will Hardy