[Answer]-Display models that includes certain values (basic search)

1👍

You can perform the search at server side, for this follow steps

  1. Put the License search box into a HTML form and add a submit button next to it.
  2. On submit call a view which will get the value entered in `license’ text box.
  3. Perform a filter on table
  4. return the results.

The first part of HTML will be changed to –

<fieldset class="module aligned ">
    <div class="form-row field-name">
        <div>
            <form method="post">
                <label class="required" for="id_name">License:</label>
                <input class="vTextField" id="id_license" maxlength="255" name="license" type="text" value="">
                <button type="submit">Search</button>
            </form>
        </div>
    </div>
</fieldset>

And view will look something like this –

def home(request):
    ...
    if request.POST:
        license_text = request.POST.get('license', '')
        machines = Machine.objects.filter(licenses__istartswith=license_text) #or you can use __in operator
    else:
        machines: Machine.objects.all()

    return render_to_response("inventory/home.html", {'machines': machines})

No other changes are required. Code not tested but it should work

Leave a comment