[Fixed]-Django Search bar feature not working?

1👍

Since HTML input elements do not have an action attribute, remove action={% url 'search_posts' %} from <input> and place it inside the <form> like this:

<form action={% url 'search_posts' %} method="GET" ...>

Also remove the type="text/submit" from the input and replace it with just text (in order to present the user a text input box to write the query – thanks Alasdair for that – haven’t seen that), like this:

<input type="text" ...>

so, the form should look like this:

<form method="GET" action={% url 'search_posts' %} class="navbar-form navbar-left">
    <div class="form-group">
        <label class="control-label" for="search-field">
        <i class="glyphicon glyphicon-search"></i></label>
        <input type="text" value="{{request.GET.q}}" name="q" class="form-control search-field" placeholder="Search Airline or Aircraft" id="search-field" />
    </div>
</form>

Concerning the other part of your question, in order to combine two (or more) QuerySets (I can’t imagine why you would want to do that), here it is:

qs = list(Aircraft.objects.all()) + list(Airline.objects.all())

Finally in your search_post.html do this:

{% if aircraft.exists %}
    <div class="team-boxed">
        <div class="container">
            <div class="row aircraft">
                {% for aircraft in aircraft %}
                    <div class="col-lg-offset-0 col-md-4 col-sm-3 item">
                        <div class="box"><img src="{{ aircraft.image.url }}"></div>
                        <h3 class="name"><a href="{{ aircraft.get_absolute_url }}">{{ aircraft.name }}</a></h3>
                        <h4><em>Range: {{ aircraft.maximum_range }} NM</em></h4>
                        <h4><em> Passengers: {{ aircraft.passengers }}</em></h4>
                        <h4><em> Speed: {{ aircraft.cruising_speed }} Kt</em></h4>
                    </div>
                {% endfor %}
            </div>
        </div>
    </div>
{% endif %}

{% if airline.exists %}
    <div class="team-boxed">
        <div class="container">
            <div class="row airline">
                {% for airline in airline %}
                    <div class="col-lg-offset-0 col-md-4 col-sm-3 item">
                        <div class="box"><img src="{{ airline.image.url }}"></div>
                        <h3 class="name"><a href="{{ airline.get_absolute_url }}">{{ airline.name }}</a></h3>
                        <h4><em>Range: {{ airline.maximum_range }} NM</em></h4>
                        <h4><em> Passengers: {{ airline.passengers }}</em></h4>
                        <h4><em> Speed: {{ airline.cruising_speed }} Kt</em></h4>
                    </div>
                {% endfor %}
            </div>
        </div>
    </div>
{% endif %}
👤nik_m

Leave a comment