Wednesday, May 28, 2014

How to pass URL to a view

Hi,

Maybe there is a better way to achieve what I am trying - so any suggestion is welcome :)

I have a small view. It does nothing much - if the user clicks a button for being interested in something he is looking at - it adds to the table of interested things. e.g. as below :

@login_required
def add_interested_project(request, project_id):
    project = get_object_or_404(Project, pk=project_id)
    try:
        InterestedProject.objects.get(user__pk=request.user.id, project__pk=project.id)
    except InterestedProject.DoesNotExist:
        InterestedProject.objects.create(user=request.user, project=project)
    return HttpResponseRedirect("/projects/" + project_id)



Now this works fine if he is in the details page e.g. as per the url /projects/4 . It works as expected, I am able to add to the table and it redirects back to the same page.

However, since this is something which I can call from any place, it fails in the sense that it gets redirected back to the project details page. E.g. I have a listing of projects, on /projects/ , i click the interested button, it unfortunately instead of going back to /projects/ goes to /projects/<project_id>

The code in my template file which calls this :

<a class="label label-default" href="{% url "project.views.add_interested_project" p.project.id %}"><span class="glyphicon glyphicon-heart"></span> Interested </a>

The relevant urls.py hookup :
url(r'^(?P<project_id>\d+)/add_interested_project$', views.add_interested_project, name="interestedProjects"),


The solution seems apparent that I should just pass the current URL to the view so that I can do a redirect back to it - but I am not able to get an example of how to actually implement it.

Regards,
Vibhu

--
Simplicity is the ultimate sophistication. - Leonardo da Vinci
Life is really simple, but we insist on making it complicated. - Confucius

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPiONwmgaEG5cCO2LDOh3dJ%2BD8ViYbQvkAk34wBRgD--3e8iQQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment