Django page URL contains %3f instead of "?" sign - django

I'm using Django paginator in CBV, and after form submit (creates new post) I'm trying to redirect to actual page with newly created post. I'm using reverse_lazy with keyword argument for page number, but in URL generated with reverse_lazy, ? sign on the beginning is changed to %3F, e.g. ?page=7->%3Fpage=7. And consequently, I get redirected to first page.
My URL path:
path("homeT/?page=<int:num>", views.homeTestView.as_view(), name="actual_page"),
I use reverse_lazy like this:
return reverse_lazy("actual_page", kwargs={'num': page_num})
P.S. And is there a easier way to get redirect to page with newly created post/comment?? Thank you.

Ok, i solved this with using such "hardcoded" approach:
return reverse("homeT") + "?page=%s" % page_num.
And redirect to page with newly created post is described
Here

Related

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.

Propagating next parameter in login in oauth2app

I have a Django Web site where I'm using the oauth2app library for OAuth 2 authentication. I've also modified oauth2app to use crispy_forms, instead of the deprecated uni_form.
The authorize() method in oauth2app is guarded by a Django decorator, #login_required. Indeed, if a client tries to authorize without first logging in, the login page appears.
As expected, the decorator causes the login page URL to have a "next" CGI parameter with the original URL for the authorization request.
The problem is, the "next" parameter is not propagated when the template for the login page is instantiated. What I'd like is for the form "action" to contain the "next" parameter.
This page purports to offer a solution:
http://django-uni-form.readthedocs.org/en/latest/helpers.html
in the section "Manipulating a helper in a view".
Following that example, I tried:
form = LoginForm()
redirect_url = request.GET.get('next')
if redirect_url is not None:
form.helper.form_action = reverse(login) + '?next=' +
redirect_url
But the source for the resulting page shows the original "action" for the form. The assignment to the form_action appears not to take effect. Indeed, if I don't instantiate the template, but just return the form_action, it hasn't changed:
return HttpResponse('action: ' + form.helper.form_action)
Color me puzzled.
The author of uni_forms clued me in here.
The problem is that the form helper was a property, so that the form_action was a fixed piece of that property.
By changing the property to a method that creates the helper, the form_action can be dynamically modified.
Everything works now, no Javascript hacks needed.

Url mismatch, django template link

I've got a prob with a link in the sidebar of my django site, in the template it's like that:
<li>Profile</li>
while in the urls.py:
url(r'^(?P<user_id>\d+)/profile/$', 'auth.views.show_profile', name='profile')
When i access it from the main page with url: e.g /1001/profile/ it loads fine but when I try to access it from another subpage with url: e.g /1001/forms/profile/ i get the error: The current URL, /1001/forms/profile/, didn't match any of these. How can i fix this?
It is because "profile" is a relative URL, and a relative URL is appended to the current URL - the resulting address is not valid across the whole site. Seems like you should use an absolute URL in your case.
At the template you can try something like:
Profile
UPDATE
To get request available in templates you have to add django.core.context_processors.request to TEMPLATE_CONTEXT_PROCESSORS. I'm not sure if it is added by default.
You must have to add your second subpage url in urls like you did for /1001/profile/
url(r'^(?P<user_id>\d+)/form/profile/$', 'auth.views.show_profile', name='profile_form')
and also correct your code as #Paulo mentioned or you can also do it through reverse url.
Profile

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: Redirect to a page, then redirect back again?

As per the title: in Django views, can I redirect to a page using HttpResponseRedirect and then from that page, immediately redirect back again to the original page?
In other words, how can I get the second view to 'remember' the first one in order to redirect back there?
I want to do this to handle some LDAP authorisation.
Thanks!
You could redirect to /page2/?next=/page1/, then get the original url from the GET parameters in the view for page2.
# page2 viewl
next = request.GET['next']
return HttpResponseRedirect(next)
You probably want to avoid any session level logic. Your requirements have nothing to do with a session, so avoid using session level constructs.
You have a request level requirement, and the request level logic identified by Alasdair is what you want.
You could store the original URL in a session variable, and then pop off that value and use it to redirect back to the original page.