How to hide a View parameter from the URL in Django? - 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.

Related

How do I access my query when using Haystack/Elasticsearch?

I originally followed this tutorial (https://django-haystack.readthedocs.org/en/latest/tutorial.html), and have so far been able to highlight my query within my returned results. However, I want to highlight this same query when visiting the next page that I load with a separate template. Is there any way to save/access this query so that I can highlight the same results within this other template?
Whenever I try and include a statement like this, I get an error, which I'm thinking is because I'm not trying to access the query properly.
{% highlight section.body with query html_tag "span" css_class "highlighted" %}
You have to send to the next page, the information that you use to highlight the results in the first page. You can use the request.session to store the data and call it in the next page, or you can send the sqs by the url to the next page.
If you want to know how to manage the search query set, and how to edit that kind of stuff, I recommend you to read the views.py forms.py and the elasticsearch_backend in the haystack folder at: "/usr/local/lib/python2.7/dist-packages/haystack"
This is the url for the documentation of Django Session: Django Session
This is the url for the documentation to pass parameters trhough url: URL dispatcher

'Hiding' form query from URL (Django 1.3)

I have a form with 6-7 fields. After user input, my webapp searches for those fields in a database and displays the results.
Now the issue is, that the URL ends up having all the form field names and their values in it.
result/?name=lorem&class=arc&course=ipsum
Now with the form having 7-8 fields the url ends up looking ugly.
Is there a Django technique to 'hide' these from the URL? Quotes around hide because I'd be okay with a completely different way to pass the objects to my database from the form as well.
Use a POST request. Here's the django docs on forms and a specific example using POST>. HTML-wise, all you need to do is change the method on the form tag.
I do not recommend to use POST requests for search. If you'll use GET it will be easer for user, he can just bookmark a link and save search or share search results with friends.

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.

Append value from cookie to URL in 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.

How to make fields readonly while updating

I have a form. Once the form is filled I don't want the user to change anything in the form.
But the user can see the values. Meaning all the fields are non editable. I can do this by using instance method but this does not help in foreignkey.
Depends on what you mean "once the form is filled".
If it's an html form post, just render a new html page with simple text and values of the submitted form.
If the post-back was an ajax call, you can change the CSS-styling of the elements for example and disable the submit button or erase the whole and substitute the values that you get back from ajax request.
There is no "editable=False" property on html input elements btw.
you can use readonlyAdmin