Append value from cookie to URL in Django - django

I have a value the user sets via a cookie and I want this value to be appended onto the URL after the user sets it. How can I enable this? Any ideas are welcome as I'm not so sure how to go about this.

This seems like an ineffective solution, but what if you write a view to check for the existance of the cookie, if it doesn't exist let them set it. If it does exist, HttpRedirect them to url + cookie-value. And add a line in your urls.py to match those urls.

Related

How to hide a View parameter from the URL in Django?

I have a view which displays the objects of a model. The objects are sorted using a key which I want to have as a view parameter so that I can create a system where the user gets to pick on how to sort the query sets.
I currently have it set up as follows:
def comics(request, sorting_key):
comics = Comic.objects.all().order_by(sorting_key)
...
However, the problem is that I need to include the sorting key somewhere in the url as well, and I don't want that. Is there any way to get around this problem? Or am I stuck with an URL that explicitly shows the sorting key?
You can make use of cookies, you can set cookie on client browser when user changes the parameter of sorting and reload the page to sort the data, which will call the same view again, read the cookie from request in your view and sort on that parameter.
Do take care of default parameter when cookie is not set or sorting on that key is not available, because user can change or delete the cookie.
You can replace the <a> tags in the menu links with <form> tags with the same URL, and send the sorting key using post, and read it from request.POST in the view.

How to remove the ?q= from search query url

I have a search field in my app, while make search the url become /search/?q=12344/ but instead i need /search/12344/ how i can achieve this
You will have to change you urls.py file to make that happen.
url(r'^search/(?P<search_term>[\w-]+)/$', views.search)
and in your views file where you catch that url request you will need to add another parameter after request like this
def search(request, search_term):
enter code here
All you need to do is create a url for the same and when a user enters anything in the search query just redirect it to your new url appending your query term to it.
For example :
url(r'^search/(?P<search_term>[\w-]+)/$', views.search)

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.

How to organize URLs in django for views handling GET data and parsing URL?

I have a view that displays some movie data. I thought that it might be a good idea to have a view handle a an URL like movie-id/1234 to search for movie id 1234. Furthermore I would like to be able to enter the ID into a form and send that to a server and search for it. To do that I created a second entry in the urls.py file shown below.
urlpatterns = patterns('',
url(r'movie-id/(?P<movie_id>.+?)/$', 'movieMan.views.detailMovie'),
url(r'movie-id/$', 'movieMan.views.detailMovie', name='movieMan.detailMovie.post'),
)
So if I want to pass data to my view either via a URL or a GET or POST request I have to enter two urls or is there a more elegant way? In the view's code I am then checking if there is any GET data in the incoming request.
To make the second url usable with the template engine, where I wanted to specify the view's url using the {% url movieMan.detailMovie.post %} syntax I had to introduce a name attribute on this url to distinguish between these two.
I am not sure if I am thinking too complicated here. I am now asking myself what is the first url entry good for? Is there a way to get the URL of a movie directly? When do these kinds of URLs come into play and how would they be generated in the template ?
Furthermore I would like to be able to enter the ID into a form and
send that to a server and search for it.
Is this actually a search? Because if you know the ID, and the ID is a part of the URL, you could just have a textbox where the user can write in the ID, and you do a redirect with javascript to the 'correct' URL. If the ID doesn't exist, the view should return a Http404.
If you mean an actual search, i.e. the user submitting a query string, you'll need some kind of list/result view, in which case you'll be generating all the links to the specific results, which you will be sure are correct.
I don't think there is a more elegant way.
I did almost the same thing:
url( r'^movies/search/((?P<query_string>[^/]+)/)?$', 'mediadb.views.search_movies' ),
The url pattern matches urls with or without a search parameter.
In the view-function, you will have to check whether the parameter was defined in the url or in the query string.

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.