"""
Views for the rdap_explorer project, query app.
"""

import ipwhois

from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.views.decorators.cache import cache_page
from json import dumps

from .forms import QueryForm


def index(request):
    if request.method == 'POST':
        form = QueryForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect(reverse(
                'query:results',
                args=(form['query'].value(),)
            ))
    else:
        form = QueryForm()

    return render(request, 'query/index.html', {
        'title': 'Query',
        'form': form
    })


@cache_page(86400)
def results(request, query):
    title = 'Results'
    error = None
    result = {}

    form = QueryForm(initial={"query": query})
    try:
        ip = ipwhois.IPWhois(query)
        result = ip.lookup_rdap(retry_count=1, depth=2, inc_raw=True)
        title = ip.address_str
    except (ValueError, ipwhois.exceptions.IPDefinedError) as e:
        error = e
        title = 'Error'
    return render(request, 'query/index.html', {
        'title': title,
        'error': error,
        'form': form,
        'result': dumps(result)
    })

"""
Views for the rdap_explorer project, query app.
"""

import ipwhois

from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.views.decorators.cache import cache_page
from json import dumps

from .forms import QueryForm


def index(request):
    if request.method == 'POST':
        form = QueryForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect(reverse(
                'query:results',
                args=(form['query'].value(),)
            ))
    else:
        form = QueryForm()

    return render(request, 'query/index.html', {
        'title': 'Query',
        'form': form
    })


@cache_page(86400)
def results(request, query):
    error = None
    result = {}

    form = QueryForm(initial={"query": query})
    try:
        ip = ipwhois.IPWhois(query)
        result = ip.lookup_rdap(retry_count=1, depth=2, inc_raw=True)
    except (ValueError, ipwhois.exceptions.IPDefinedError) as e:
        error = e
    return render(request, 'query/index.html', {
        'title': 'Results',
        'error': error,
        'form': form,
        'result': dumps(result)
    })

--- 
+++ 
@@ -32,6 +32,7 @@
 
 @cache_page(86400)
 def results(request, query):
+    title = 'Results'
     error = None
     result = {}
 
@@ -39,10 +40,12 @@
     try:
         ip = ipwhois.IPWhois(query)
         result = ip.lookup_rdap(retry_count=1, depth=2, inc_raw=True)
+        title = ip.address_str
     except (ValueError, ipwhois.exceptions.IPDefinedError) as e:
         error = e
+        title = 'Error'
     return render(request, 'query/index.html', {
-        'title': 'Results',
+        'title': title,
         'error': error,
         'form': form,
         'result': dumps(result)
Change results page title to include query (or "Error" on error).
