Built in views

Dave Hall edited this page Aug 4, 2015 · 7 revisions

Built-in views

django-watson comes with a built-in search view to make building a site-wide search easy.

To use it, add the django-watson urls to your site's urlconf:

url(r"^search/", include("watson.urls", namespace="watson"))

You can then perform a search by including a form in your template as follows:

<form action="{% url 'watson:search' %}">
    <input name="q" value="{{request.GET.q}}">
    <input type="submit" value="Go">
</form>

The built-in search view will display a list of results taken from your site-wide set of registered models. You can customize the way it renders as follows:

  • context_object_name: The name of the variable to contain the list of results (default 'search_results')
  • exclude: The list of models to exclude from search results (default empty list)
  • empty_query_redirect: The page to redirect to if the user enters an empty search term (default None)
  • extra_context: A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the view will call it just before rendering the template.
  • models: The list of models to search from (default all)
  • paginate_by: An integer specifying how many objects should be displayed per page (default None)
  • page: An integer specifying the page to display, or 'last'. If blank, then the GET parameter 'page' will be used (default None)
  • query_param: The GET parameter to use for the search term (default 'q').
  • template_name: The name of the template used to render the search results (default 'watson/search_results.html')

For more information about passing arguments to view functions, see the Django URL dispatch documentation.

For more information about using generic views, see the Django generic views documentation.