0

I am trying to use the Django message system to send the user back to their previous page. I have a Delete Customer FBV that can be accessed from multiple locations and after deleting the customer I want to send the user back to where they were. If I do this it works well and I get the behavior I expected:

@login_required
def delete_customer(request, custpk):
    del_cust = Customer.objects.get(pk=custpk)
    returnURL = ''

    if request.method == "POST":

        storage = get_messages(request)

        for message in storage:
            returnURL = message.message
        storage.used = True

        del_cust.delete()

        return redirect(returnURL)

    context = {
        'del_cust': del_cust
    }

    return render(request, 'delete_customer.html', context)

However, I want to be able to use that returnURL variable both in the If "POST" section and in the context variable to send to the render statement. Why when I do this does it not work?

@login_required
def delete_customer(request, custpk):
    del_cust = Customer.objects.get(pk=custpk)
    returnURL = ''

    storage = get_messages(request)

    for message in storage:
        returnURL = message.message
    storage.used = True

    if request.method == "POST":

        del_cust.delete()

        return redirect(returnURL)

    context = {
        'del_cust': del_cust,
        'returnURL': returnURL
    }

    return render(request, 'delete_customer.html', context)

Why does this not work? When the redirect hits it says there is nothing to redirect to. How can I set up the returnURL variable at the top of the function and use it in the If "POST" section and in the context variable?

EDIT: I agree, assigning the variable in a loop is bad. Consider the example below. Why is the del_cust variable available within the If..."POST" statement but the urlList variable is not? The first print(urlList) shows me my list with one item (the text of my one message). The second print(urlList) inside the If..."POST" statement shows me an empty list. Why?

@login_required
def delete_customer(request, custpk):
    del_cust = Customer.objects.get(pk=custpk)
    urlList = []
    storage = get_messages(request)

    for message in storage:
        urlList.append(message.message)
    storage.used = True

    print(urlList)

    if request.method == "POST":

        print(urlList)

        del_cust.delete()

        return redirect('/')

    context = {
        'del_cust': del_cust
    }

    return render(request, 'delete_customer.html', context)
Tuffgong
  • 57
  • 7
  • Have you verified that `returnURL` is set to a valid value? – schillingt Apr 16 '20 at 18:58
  • Most basic problem of this code is that if `storage` is an iterable datatype then value set to variable `returnURL` is going to be just the last value from storage. Also return url needs to be a valid url or object. – MohitC Apr 16 '20 at 19:19
  • I guess because non-POST requests do not bear any `messages`. Also an assignment to a scalar variable _inside loop_ does not look well. – Ivan Starostin Apr 16 '20 at 19:19
  • MohitC, the value is just the last value from storage. I am not using storage for anything else. Also, doesn't storage.used=True clear the messages? There should only be one message in there at a time. returnURL is valid, the code works in the first version. – Tuffgong Apr 16 '20 at 19:38
  • schillingf, returnURL is valid because the first version works perfectly and returns me to the page from which I came. – Tuffgong Apr 16 '20 at 19:39
  • Ivan, I agree the way the variable is assigned could be improved. My plan is to only have one message in there at a time and my inexperience with retrieving messages is partly to blame. However, in the second version (the one that doesn't work) if I print returnURL before the If request.method == "POST" statement, I get the expected result, if I print the variable again, 2 lines later inside the If.."POST" statement it is empty! How can this be? It is not a scope issue. – Tuffgong Apr 16 '20 at 19:43

0 Answers0