A simple form-handling example

Continuing the ongoing example of books, authors and publishers, let's create a simple view that lets users search our book database by title. Generally, there are two parts to developing a form: the HTML user interface and the backend view code that processes the submitted data. The first part is easy; let's just set up a view that displays a search form:

 
from django.shortcuts import render 
 
def search_form(request): 
    return render(request, 'search_form.html') 

As you learned in Chapter 3, Templates, this view can live anywhere on your Python path. For sake of argument, put it in books/views.py. The accompanying template, search_form.html, could look like this:

<html> <head> <title>Search</title> </head> <body> <form ...

Get Mastering Django: Core now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.