'Hiding' form query from URL (Django 1.3) - django

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.

Related

Setting html-attributes to HTML forms using serializer in Django

I want to make page with form that contains any kinds of inputs. I'm using Django and rest framework. I've succeded in representing HTML form using rest framework {% render_form %} tag and serializer with some fields. But i'm just able to set few attributes, for instance, placeholder and input_type. And it works well. The next thing I want is setting some #id, .class and any attributes to input tags(or select) on a server side because I need to handle these HTML forms using knockoutjs or JQuery but I can't do it without data-bindings or #IDs. I couldn't find any information about it. I guess it is possible to set any attributes to inputs on client-side finding them by label name but it seems a bad way. Or maybe I could get list of fields and just represent it in html template? Are there some pieces of advice?

Django admin filter urls for not-equals

I've created a custom FieldListFilter for my site's admin for whether modeltranslation translations are available for a field.
It creates a filter that currently looks like this on the sidebar:
The "Lacks English" option adds vt_title_en__exact= to the query string.
I would like to also implement a "Has English" which does the opposite filter. Can I do that with a django admin site url query strings?
Btw: source for my custom filter: https://github.com/yairchu/azlemi/blob/master/vote/admin.py#L30
I think vt_title_en__gt= can do the job. Any non-empty string is greater than "".
You should create queryset method for do it, because there is no such possibility to put exclusion or negation into expected_parameters.

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

How to create a WordPress like URL naming convention in Django?

I'm a newbie in Django and in WordPress if you create a Post called "hello world" then the URL by default will be like
wordpress.com/2012/07/05/hello-world/
and if you create another post with the same name it will be
wordpress.com/2012/07/05/hello-world-2/
I want to achieve the same in Django and I was thinking to create a sample urlconf like this
(r'^articles/(\d{4})/(\d{2})/(?P<name>\w+)', 'article.views.article_detail')
and in the views break down the name and iterate through all the items and match the name.
But the problem with will be that I won't be able to reference posts dynamically. For e.g. if I was to link the a hello world post I would need to find out how many posts with the same name exist already and then append the additional number to it which is inefficient.
So what's the best way to do this in Django?
See the documentation for Django's {{ url }} template tag. It lets you pass it a view name and parameters, and automatically generates the correct URL for you.
You can take care of appending numbers to each post's name in the function that generates its slug - you could have a look at django-autoslug

Django: Pagination with urls.py

I'm building a Blog in Django (using Generic Views) and I use the same template for both my date based and list detail views. I'm trying to setup pagination, but I want to do so with URL patterns rather than using an ugly ?page=1 url suffix.
The problem is in the actual html template, I cannot find a way to determine which view was used to render the page, so while I have access to all the pagination stuff, I have no way to generate the appropriate URL.
In other words, if the view was rendered by my archive_month(request, month, year, page=0) view, I would need to structure the URL for the next and previous pages as /blog/dec/2009/PageX/, versus the blog index, which would mean the URL would be /blog/pageX/.
Well I just realized that date_based generic views don't support pagination, so problem solved.