Views

Class Diagram

Inheritance diagram of django_select2.views

Reference

django_select2.views.NO_ERR_RESP = 'nil'

Equals to ‘nil’ constant.

Use this in Select2View.get_results() to mean no error, instead of hardcoding ‘nil’ value.

class django_select2.views.JSONResponseMixin[source]

Bases: object

A mixin that can be used to render a JSON response.

response_class

alias of HttpResponse

render_to_response(context, **response_kwargs)[source]

Returns a JSON response, transforming ‘context’ to make the payload.

convert_context_to_json(context)[source]

Convert the context dictionary into a JSON object

class django_select2.views.Select2View(**kwargs)[source]

Bases: django_select2.views.JSONResponseMixin, django.views.generic.base.View

Base view which is designed to respond with JSON to Ajax queries from heavy widgets/fields.

Although the widgets won’t enforce the type of data_view it gets, but it is recommended to sub-class this view instead of creating a Django view from scratch.

Note

Only GET Http requests are supported.

respond_with_exception(e)[source]
Parameters:e (Exception) – Exception object.
Returns:Response with status code of 404 if e is Http404 object, else 400.
Return type:HttpResponse
check_all_permissions(request, *args, **kwargs)[source]

Sub-classes can use this to raise exception on permission check failures, or these checks can be placed in urls.py, e.g. login_required(SelectClass.as_view()).

Parameters:
  • request (django.http.HttpRequest) – The Ajax request object.
  • args – The *args passed to django.views.generic.View.dispatch().
  • kwargs – The **kwargs passed to django.views.generic.View.dispatch().

Warning

Sub-classes should override this. You really do not want random people making Http reqeusts to your server, be able to get access to sensitive information.

get_results(request, term, page, context)[source]

Returns the result for the given search term.

Parameters:
  • request (django.http.HttpRequest) – The Ajax request object.
  • term (str) – The search term.
  • page (int) – The page number. If in your last response you had signalled that there are more results, then when user scrolls more a new Ajax request would be sent for the same term but with next page number. (Page number starts at 1)
  • context (str or None) –

    Can be anything which persists across the lifecycle of queries for the same search term. It is reset to None when the term changes.

    Note

    Currently this is not used by heavy_data.js.

Expected output is of the form:

(err, has_more, [results])

Where results = [(id1, text1), (id2, text2), ...]

For example:

('nil', False,
    [
    (1, 'Value label1'),
    (20, 'Value label2'),
    ])

When everything is fine then the err must be ‘nil’. has_more should be true if there are more rows.

class django_select2.views.AutoResponseView(**kwargs)[source]

Bases: django_select2.views.Select2View

A central view meant to respond to Ajax queries for all Heavy widgets/fields. Although it is not mandatory to use, but is immensely helpful.

Tip

Fields which want to use this view must sub-class AutoViewFieldMixin.

Table Of Contents

Previous topic

Fields

Next topic

Util

This Page