0

In order to pass a function with arguments inside one of my template, I used a hidden input that redirects to a view where the function is (the function is used to delete a user the user is "following"). This view then redirects to the previous page thanks to a HttpResponseRedirect(reverse('myview', args=[a number])).

The thing is that the page I redirect to needs an argument (a number). And I would like the user to be redirected to the exact page where he was.

Here is my template:

    <form method="post" action="{% url projet.views.remove_relationship %}">
    {% csrf_token %}
    <input type="hidden" name="remove_relationship" value="{{ user.id }}">
    <input type="submit" value="delete" />

And the view:

   def remove_relationship(request):
      user = request.user
      if request.method == 'POST':
        user_id = request.POST['remove_relationship']
        user_id= int(user_id)
        user_name = User.objects.get(id=user_id)
        user.userprofile.remove_relationship(person=user_name.userprofile, status=1)
     return HttpResponseRedirect(reverse('myoriginalview', args=[A NUMBER]))

The page where the user comes from is like: www.mysite/myoriginalview/ANUMBER. I want to redirect him to this exact page

In the original view, the number corresponds to an object id. (match.id).

So what I wan't to do is getting the match.id of my original view, pass it in my view remove_relationship in order to use it in "args=[match.id]".

So how do I get this ID from my original view and pass it into my new view?

Here is what I tried:

in my models.py:

@models.permalink
def get_absolute_url_remove_relationship(self):
    return ('remove_relationship', (), {'dub_id': self.id})

use it in my template:

<form method="post" action="{{get_absolute_url_remove_relationship}}">
    {% csrf_token %}
    <input type="hidden" name="remove_relationship" value="{{ user.id }}">
    <input type="submit" value="delete" />

In my view:

def remove_relationship(request, match_id=None):
match_id= int(match_id)
user = request.user
if request.method == 'POST':
    user_id = request.POST['remove_relationship']
    user_id= int(user_id)
    user_name = User.objects.get(id=user_id)
    user.userprofile.remove_relationship(person=user_name.userprofile, status=1)

return HttpResponseRedirect(reverse('terrain', args=[match_id]))

And I do the necessary modifications in my url.py

So when the user clicks on the button "delete" to delete a user he is following, the redirection works well, but the user is not deleted. It looks like the function is not used anymore, while when I used {%url projet.views.remove_relationship %}, the user was deleted, but there was no redirection.

Any help would be very welcome.

PS: The question comes after an other question I asked before (http://stackoverflow.com/questions/13233794/function-with-arguments-in-a-template-django). I opened a new topic in order to be clearer.

Juliette Dupuis
  • 1,027
  • 2
  • 13
  • 22

1 Answers1

0

You need to pass the args as a tuple while invoking reverse. Also make sure that you use the fully qualified name of the view inclusive of the the app name.

return HttpResponseRedirect(reverse('appname.views.viewname', args=(match_id,)))

Reference

Pratik Mandrekar
  • 9,362
  • 4
  • 45
  • 65
  • Thank you, but the problem is stil the same. The redirection works, but the function doesn't works. The user is not deleted. – Juliette Dupuis Nov 05 '12 at 17:26
  • Have you tried the code to delete the relationships at the python interpreter? If it gets deleted there, then ensure that the parameter is getting passed correctly using a print in debug mode. – Pratik Mandrekar Nov 05 '12 at 17:31
  • Thank you for your help. What I finally did is simply using an other hidden input, with the value={{match.id}}, and I used request.POST on my remove_relationship... – Juliette Dupuis Nov 05 '12 at 17:55