简体   繁体   中英

Querying models not working correctly - Django

I am currently learning Django and as an exercise I have written a simple model, a view and couple of templates. The purpose of my app is to query the books in my database that contain a specific term in their 'title' field. I have written a 'Book' model for the same, a view function to query the books using the 'Book' model and return the results to a template 'search-results.html'. The query term is passed from an HTML form. I am able to receive the query string from the 'request' object, but when I query the 'Book' model for results, I get an empty list. I tried querying the 'Book' model from the python shell and it works perfectly fine. Why am I not getting any result from my view function? The following is the code in my view function:

def search(request):
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        books = Book.objects.filter(title__icontains=q)
        return render(request,'search-results.html',{'results':books,'query':q})
    else:
        message = 'You submitted an empty form'
    return HttpResponse(message)

The following is the code for my search form:

<form action='/search/' method='get'>
        <input type='text' name='q'>
        <input type='submit' value='Search'>
</form>

The query term I used is 'Django'. The following is the table data:

mysql> select * from books_book;
+----+-----------------+--------------+-----------------+
| id | title           | publisher_id | publicationDate |
+----+-----------------+--------------+-----------------+
|  1 | Learning Python |            1 | 2014-09-02      |
|  2 | Learning Django |            2 | 2014-09-28      |
+----+-----------------+--------------+-----------------+
2 rows in set (0.00 sec)

The following is the results template:

<body>
        <p>
            The following are the {{books|length}} results for the search term {{query}}:
            {% for book in books %}
                <li>{{book.title}}</li>
            {% endfor %}
        </p>
    </body>

I am using Django 1.6 and Python 2.7.8, MySQL 5.6

Thank you, Rakesh.

You are querying over books in your templates, whereas you name the variable results when you pass it to your template in the view.

In your template try iterating over results instead of books :

{% for book in results %}
    <li>{{book.title}}</li>
{% endfor %}

which would result in:

<body>
    <p>
        The following are the {{results|length}} results for the search term {{query}}:
        {% for book in results %}
            <li>{{book.title}}</li>
        {% endfor %}
    </p>
</body>

You called your context variable containing the books "results", but in the template you are iterating through "books". Pick one and be consistent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM