Wednesday, May 28, 2014

Re: How to pass URL to a view

Thanks Ilya ! Perfect ! That is what I was missing.

And point noted about reverse.

V.


On Wed, May 28, 2014 at 6:04 PM, Ilya Kazakevich <Ilya.Kazakevich@jetbrains.com> wrote:
Hello,

1) Never hardcode urls in Django. Use reverse: https://docs.djangoproject.com/en/dev/ref/urlresolvers/#django.core.urlresolvers.reverse
2) If I understood you correctly, you need to redirect user back. Use request header REFERER' (check rfc2616 section 14.36). For Django: http://stackoverflow.com/questions/12758786/redirect-return-to-same-previous-page-in-django
See also: https://docs.djangoproject.com/en/dev/ref/request-response/

Be sure to check this header exists.


Ilya Kazakevich,
JetBrains PyCharm (Best Python/Django IDE)
http://www.jetbrains.com/pycharm/
"Develop with pleasure!"


>-----Original Message-----
>From: django-users@googlegroups.com
>[mailto:django-users@googlegroups.com] On Behalf Of Vibhu Rishi
>Sent: Wednesday, May 28, 2014 4:23 PM
>To: django-users@googlegroups.com
>Subject: 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/CAPiONwmgaEG5cCO2LDOh3
>dJ%2BD8ViYbQvkAk34wBRgD--3e8iQQ%40mail.gmail.com
><https://groups.google.com/d/msgid/django-users/CAPiONwmgaEG5cCO2LDOh
>3dJ%2BD8ViYbQvkAk34wBRgD--3e8iQQ%40mail.gmail.com?utm_medium=emai
>l&utm_source=footer> .
>For more options, visit https://groups.google.com/d/optout.
>
>
>No virus found in this message.
>Checked by AVG - www.avg.com
>Version: 2014.0.4592 / Virus Database: 3950/7573 - Release Date: 05/27/14
>
>________________________________
>
>No virus found in this message.
>Checked by AVG - www.avg.com
>Version: 2014.0.4592 / Virus Database: 3950/7563 - Release Date: 05/26/14


--
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/008e01cf7a71%241d85e100%245891a300%24%40JetBrains.com.
For more options, visit https://groups.google.com/d/optout.



--
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/CAPiONwmYfoKzCDCiiS6KTLM%2BG6iDvcYbnWJkiAFTJezsSvfV7A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment