Make a link that maintains get params? - django

I've got an HTML table that lists a bunch of entries. I want to make the column headers clickable to be sorted. Problem is, I already have a bunch of filters in the URL (stuff like ?min_price=200&max_price=6000). How can I build the link such that it keeps these params in tact? Is there a function for it? Do I have to do it in the view?

Here's a cute trick I found that might do what you want:
http://andrewwilkinson.wordpress.com/2009/02/03/using-django-forms-for-get-urls/
Personally, I wouldn't do it that way.
I'd use Javascript and change the DOM directly.
You're not adding or removing any data if you are just sorting.
If you just change the DOM you'll possibly avoid calling the server, and the user experience will be better because you won't need to reload the screen.

In your template, you can add:
your_current_url?{{ request.META.QUERY_STRING }}
to pass the current query string params on to additional links.
Hope that helps!

Related

Backbone, selectively render parts in template

I am listing products as table rows, each row contains input fields for specifying the quantity of products.
I made a Fiddle for it here, http://jsfiddle.net/kroofy/4jTa8/19/
As you can see, after the input in the Qty field have been made, the whole row render again. And because of that the focus of the input field will be lost, which is not good if you want to input more than just one digit, or tab between input fields.
What would be the most elegant way to solve this?
I would handle this by setting model.set({qty: _qty}, {silent: true}) and then updating the non-input fields with this.$.
As an alternative to the silent treatment: rather than listening for change events, listen for change:qty and change:sellPrice and have a method that updates just the HTML that needs updating within this.$, rather than re-rendering the DOM object and breaking your focus.
Either way, your comment about "selective updating" on the fiddle is certainly the right way to go.
(this.$ is a backbone hack to jQuery that restricts all selectors to search only within the DOM of the View's element. The element doesn't even need an ID or class; it just needs to exist and the View maintains a handle to it. It's incredibly useful.)
i built a plugin for backbone called Backbone.ModelBinding that may be able to help in this situation. my plugin allows you to update portions of a view with data from a model, when the model changes.
i've forked / updated your fiddle to show it in action: http://jsfiddle.net/derickbailey/FEcyF/6/
i removed the binding to the model change. i've also added id attributes to the inputs of the form to facilitate the plugin (the attribute that the plugin uses is configurable, though). and lastly, i've added a data-bind attribute to the sell price total td.
you can get the plugin here: http://github.com/derickbailey/backbone.modelbinding/
hope that helps
FWIW: my plugin is an automated version of what Elf is suggesting. I've written code exactly like he is describing, numerous times, which is where the plugin came from. I just got tired of writing that code by hand :)

<cfform> inside cfscript component

I'm making components for a site and I'm wondering if I can return a cfform inside a return variable from a component and force coldfusion to output it parsed.
Obviously using "writeOutput(")" doesn't work.
How could I achieve this?
Thanks for your time!
You can't return a cfform, because tags can't be used inside of a CFScript based component. You're far better off doing something like this with a custom tag, which then references your component to get pieces to build out the form.
I would avoid (if at all possible) putting any cfform related pieces into a component, script-based or not.
If you did want to ultimately go this route, you'd need to put the cfform (and it's relevant pieces) either in another component that gets called by the script based one, or in an include that then is saved to a variable. All of the solutions related to trying to get the cfform into your CFC are going to be messy.
If you absolutely must do this (though I would shy away from it myself) you might want to have a look at this:
http://www.madfellas.com/blog/index.cfm/2011/1/26/Using-CFML-tags-in-cfscript-C4X-prototype

drupal theming a view for Customfield: PHP code

I have a view called "contests_slider" with a block display. I'm hiding all fields and using a "Customfield: PHP code" field instead which calls a function called display_front_contests(). In that function, querying the database and building some html and returning it. I'm displaying the output in a block. The problem is Drupal is adding alot of extra divs that I don't want. I went to "Theme: Information" and copied the theme "views-view-field.tpl.php" to "views-view-field--contests-slider--block-1--phpcode.tpl.php" and put just: in it and it's still outputting all the extra html.
Any ideas? am I using the wrong template?
If you are only using views to create a block, but otherwise query the datebase, create the markup etc, you should consider making a block in a custom module. All the work is in the code you have already written. That way you wont have to think about the many templates that views uses, but instead you'll just use the block.tpl.php.
Take a look at hook_block for info on how to do it.

django - the best way to combine pagination with filtering and request.POST - like stackoverflow - ajax?

I want to combine pagination with filtering. Since I have a lot of filters I do not want to send them per GET request, since the URLs get really ugly.
Since django pagination uses GET request to pass the page parameters, I do not know how I can combine these two approaches.
Any idea?
Great add-on would be: How can I combine this approach with table sort? :-)
Edit:
Actually it should work like the pagination of stackoverflow - user questions. If a user clicks on a page number one is shown the correct page, without showing the get parameters in the url.
This is the url called.
https://stackoverflow.com/api/userquestions.html?page=2&pagesize=10&userId=237690&sort=Recent
But the url shown in the browser is neat and short.
Seems to be ajax. Anybody an idea how to implement this? :)
If the URL is not shown in the browser`s address bar, I do not care about whether it is beautiful or not.
Edit: The solution:
Make an ajax update with all filter parameters passed to the view. This should help you get started with implementing ajax for your site: link
Thus the GET parameters never show up in the address bar.
have you checked the paginate application for django?
it may help you a lot, use it all the time :D
http://code.google.com/p/django-pagination/
Have you considered django-tables2? It gives you django-admin style tables without you having to write the logic yourself.
maybe you can use the urs, something like:
http://oursite.com/something/filter1/filter2/3/
the doc -> http://docs.djangoproject.com/en/1.1/topics/http/urls/
I figured out two solutions:
Instead of using just hyperlinks use it inside a POST form, i dont have any example now but i remember have used that for REST functions in Ruby on rails
Save the query info in a session.
Hope this help.

Multiple views loaded into a template

Hay guys, in PHP when i want to load a differnet page (render, with all variables outputting) i would use
include 'mypage.php'
This would load the page up and render the output.
How do i do something like this in Django? My problem is that i have a "quick list" which lists a bunch of popular items. I want this list to appear on every page, even if i don't call the object.
any ideas?
EDIT: The view is called shop.app.popular_items
You can use Inclusion tags.
Use template context_processors
To answer your question to becomingGuru on when to use context processors and when to use inclusion tags, remember that context processors are run on every template render. So they should only be used when you know you will always need the value.
Edit:
After Daniel's answer and as you said you want to have it on every page,
becomingGuru's answer is probably the best solution.
This does not belong into the view (if you not want to display this as a standalone page).
This should go into a custom template tag.