Accessing URL in 'get' method of view - django

I want a web page (with the url page3) to be displayed differently depending on whether a user on my website is redirected to it from the pages with urls page1 or page2.
How can I access the full url (not just the query parametres in it) from which the user was redirected in the get method in the view associated with the url page3 ?

After reading the docs more thoroughly (thanks for the tip Brandon!), I found request.META['HTTP_REFERER'] did the trick.

Related

Facebook Graph API add extra parameter to login url

is there any way to get back my users to the current page after they login and redirect to my callback url? for example my current page url is:
https://mydomain.test/post1
then my callback url is:
https:/mydomain.test/callback.php
I tried to add extra parameter to login url but got an error because of the Valid OAuth Redirect URIs url match. my url is dynamic so I need a way to set parameters that changes and start from there.

Check whether a page exists in Django CMS

I am creating a page in the admin area of Django CMS and I have the redirect field under Advanced Settings. How can I check that the URL entered in that field is a valid URL of an existing Django CMS page?
What should I test? I thought about issuing a request to that URL and if it throws a 404, then invalidate the field, but this sounds a bit too far fetched. What other options do I have?
You can check if your actual page is in a pool of pages
if your page is on the draft mode:
from cms.models import Page
your_page.get_path() in [p.get_path() for p in Page.objects.public().published()]
with a reverse_id:
your_page.get_path() in [p.get_path() for p in Page.objects.all() if p.reverse_id != your_page.reverse_id]
I've used get_page_queryset_from_path from cms.utils.page_resolver and checked that the path entered on the redirect field actually returns a valid Page using the above function.

Django. How to redirect to url with fragment identifier

For example in some cases after POST or GET request I want to redirect user not only to specific page, but to a specific part of page. What would be better: implement this in Django(reconstruct redirect url?) or implement this in javascript?
Why would you want to do it in JS? If you're redirecting to a different page already, just add #whatever to the redirect URL to go direct to the anchor.

How to redirect to page which has GET parameters after login using the {{next}} variable in django

I am using allauth to provide registration and login in my django site. Everything else seems to be working fine other than that I am having problems to redirect the person to the current page after login.
I have a page where I have some interview questions and a typical url for it would be like
/questions/?company=google
This page contains a list of questions for the company google, but to view the answer the person needs to login. The answers are displayed in a dropdown box. However when the user clicks on login a request is sent to the login page as follows
/login/?next=/questions/
And the get parameter which was actually there in my actual page is not sent because of the & in my url. How can I solve this problem. It does not look nice that the person is redirected to a different page from where he/she tried to login.
I know sending the next parameter as a GET variable is not the solution, but is there a way I can send the redirect link as a POST variable from the template.
I tried another thing, in my view that displays the questions list. I set session variables which contains the url of the current link . If a user clicks on login, in my login view I check for this particular session variable. If it is set then I redirect to that page.
However the session variable is not received in the login view, I am not sure but I think the session is reset when the user goes to the login view.
Any suggestions are appreciated.
Have you tried
next = request.get_full_path()
This will return correct path with all queries ( see docs ) , you can then pass it as GET param to redirect url e.g.
full_path = request.get_full_path()
return HttpResponseRedirect('%s?next=%s' % (reverse('login'), full_path))
You should encode the URL-parameter in this case. You want to send a variable like /questions/?company=google, but as you mentioned the ?, = (amongst others) characters are special ones. It has a special meaning when embedded in the URL. If you encode the variable with URL encoding, it becomes %2Fquestions%2F%3Fcompany%3Dgoogle. If you assign that to the parameter next, the URL becomes: /login/?next=%2Fquestions%2F%3Fcompany%3Dgoogle. This should redirect to the correct place on login.

Django changing rendered url in a view

I want to change the display URL of a rendered response object. I have a single view "view1" and it is called by a URL, "localhost/foo/view1" . On some condition in view1 I want to change rendered URL to be displayed on browser to "localhost/foo/other/view1". I don't want to use HttpResponseRedirect. I only want to change the display URL in browser when the requested page is rendered.
No way you can do that. It would be phishing paradise if anyone could change the URL without redirecting.
Use redirects instead.
It's possible with the html5 history api, http://html5demos.com/history