On Sun, Nov 25, 2012 at 3:17 AM, Darren Spruell <phatbuck...@gmail.com>wrote:

> I've got a handful of applications that feature paginated object lists
> as well as a search dialog allowing users to search for simple object
> attributes.
>
> What is the common pattern to use in Django apps to build URI query
> strings in view code that can preserve parameters for rendering links
> in templates? In my case I'd like to handle the following:
>
> 1. User searches for string, passed as GET parameter and application
> returns paginated object list the spans multiple pages
> 2. First page contains links for subsequent pages ('page' parameter in
> URI query string) and preserves search string (additional 'search'
> parameter in URI query string)
>
> So far I've taken key/value pairs out of request.GET and added them to
> the context as more_query_params and passed them in to templates. This
> seems a little raw and klugey although it works for the simple cases.
> There must be a better way. Currently (from a view that returns
> paginated object list:
>
>     # Determine other parameters from request, provide to template to craft
>     # smarter URLs that handle concurrent search + pagination (etc.).
>     if request.method == 'GET':
>         more_query_params = ''
>         for key, value in request.GET.items():
>             if key != 'page':
>                 more_query_params += '&%s=%s' % (key, value)
>     return render_to_response('submissionlog_pages.html', {
>         'submissionlogs': submissionlogs,
>         'search_form': search_form,
>         'more_query_params': more_query_params,
>     }, context_instance=RequestContext(request))
>
> What are better ways to handle this?
>
>
>
See: http://docs.python.org/2/library/urllib.html#utility-functions
Particularly urlencode.  You should be able to initialize a dictionary from
request.GET, so long as none of the keys is multi-valued, e.g.; x =
dict(request.GET)
Then you can delete the 'page' key from the dictionary: if 'page' in x: del
x['page']
Then urlencode should help.

Bill

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to