from __future__ import absolute_import, unicode_literals

from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.shortcuts import render

import wagtail
if wagtail.VERSION >= (2, 0):
    from wagtail.core.models import Page
    from wagtail.search.models import Query
else:
    from wagtail.wagtailcore.models import Page
    from wagtail.wagtailsearch.models import Query


def search(request):
    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        search_results = Page.objects.live().search(search_query)
        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, 10)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return render(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    })

from __future__ import absolute_import, unicode_literals

from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.shortcuts import render

from wagtail.core.models import Page
from wagtail.search.models import Query


def search(request):
    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        search_results = Page.objects.live().search(search_query)
        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, 10)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return render(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    })

Drop wagtail 1.13 support from example
Drop wagtail 1.13 support from example

  from __future__ import absolute_import, unicode_literals
  
  from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
  from django.shortcuts import render
  
- import wagtail
- if wagtail.VERSION >= (2, 0):
-     from wagtail.core.models import Page
+ from wagtail.core.models import Page
-     from wagtail.search.models import Query
+ from wagtail.search.models import Query
- else:
-     from wagtail.wagtailcore.models import Page
-     from wagtail.wagtailsearch.models import Query
  
  
  def search(request):
      search_query = request.GET.get('query', None)
      page = request.GET.get('page', 1)
  
      # Search
      if search_query:
          search_results = Page.objects.live().search(search_query)
          query = Query.get(search_query)
  
          # Record hit
          query.add_hit()
      else:
          search_results = Page.objects.none()
  
      # Pagination
      paginator = Paginator(search_results, 10)
      try:
          search_results = paginator.page(page)
      except PageNotAnInteger:
          search_results = paginator.page(1)
      except EmptyPage:
          search_results = paginator.page(paginator.num_pages)
  
      return render(request, 'search/search.html', {
          'search_query': search_query,
          'search_results': search_results,
      })
  
Drop wagtail 1.13 support from example
## Code Before:
from __future__ import absolute_import, unicode_literals

from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.shortcuts import render

import wagtail
if wagtail.VERSION >= (2, 0):
    from wagtail.core.models import Page
    from wagtail.search.models import Query
else:
    from wagtail.wagtailcore.models import Page
    from wagtail.wagtailsearch.models import Query


def search(request):
    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        search_results = Page.objects.live().search(search_query)
        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, 10)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return render(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    })

## Instruction:
Drop wagtail 1.13 support from example
## Code After:
from __future__ import absolute_import, unicode_literals

from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.shortcuts import render

from wagtail.core.models import Page
from wagtail.search.models import Query


def search(request):
    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        search_results = Page.objects.live().search(search_query)
        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, 10)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return render(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    })
