About this document
This document describes the functionality provided by the various python modules in Softwarefabrica Django Utils.
The usearch module implements the search technique described in Adding search to a Django site in a snap.
The get_query function builds and returns a composite query object (see the Q object documentation), using the provided search fields and query string. The query string, typically coming from a form, contains the search terms. The search fields are the model field names that are to be searched. The resultant Q object can then be applied to filter results in a normal Django query.
Example usage:
def v_search(request, template_name='wiki/search_results.html'): from softwarefabrica.django.utils import usearch query_string = '' found_pagecontents = None if ('q' in request.GET) and request.GET['q'].strip(): query_string = request.GET['q'] pagecontent_query = usearch.get_query(query_string, ['title', 'text', 'page__name', 'page__desc',]) found_pagecontents = PageContent.objects.filter(pagecontent_query).order_by('-page', '-created', '-rev') return render_to_response(request, template_name, { 'query_string': query_string, 'pagecontents': found_pagecontents, })
The viewshelpers module provides various useful functions.
This function can be used as a template context processor to automatically populate template contexts with some variables from the project settings (this requires the use of a RequestContext, see below the render_to_response convenience function on how to do it effortlessly).
To use this context processor, it's necessary to add 'softwarefabrica.django.utils.viewshelpers.context_vars' to TEMPLATE_CONTEXT_PROCESSORS in your settings file:
TEMPLATE_CONTEXT_PROCESSORS = ( ... 'softwarefabrica.django.utils.viewshelpers.context_vars', )
The variables pushed into the context are:
An extension of the Django shortcut function render_to_response, which passes a RequestContext to the template instead of a simple Context. This has the effect of forcing execution of the context processors, which put additional variables into the template context. This function is especially convenient when using the aforementioned context_vars context processor.
softwarefabrica.django.utils.viewshelpers.render_to_response renders a given template with a RequestContext built from the given context dictionary and request object, and returns an HttpResponse object with that rendered text.
The following example renders the template myapp/index.html with the MIME type application/xhtml+xml:
from softwarefabrica.django.utils.viewshelpers import render_to_response def my_view(request): # View code here... return render_to_response(request, 'myapp/index.html', {"foo": "bar"}, mimetype="application/xhtml+xml")
This example is equivalent to:
from django.http import HttpResponse from django.template import Context, loader def my_view(request): # View code here... t = loader.get_template('myapp/template.html') c = RequestContext(request, {'foo': 'bar'}) r = HttpResponse(t.render(c), mimetype="application/xhtml+xml")
The managers module provids generic custom model managers.
The QuerySetManager is the generic model manager described in jQuery style chaining with the Django ORM.