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.