I don't know exactly what the tutorial says to do, but it seems to me
that the problem is with this line:

    if 'q' in request.GET and request.GET['q']:

If you've entered an empty query -- "" -- then the first condition
('q' in request.GET) would be true, but the second condition
(request.GET['q']) would evaluate to false, since an empty string is
treated as false. So the code within the if statement would never
execute, the and the variable error would still be set to false when
you render the search_form.html template.

The solution is simply to take out the second condition in that
statement, so it reads "if 'q' in request.GET:" ... then the next if
statement ("if not q:") takes care of testing whether you entered an
empty string.

Hope it helps
jeff


On Apr 28, 1:32 am, 83nini <83n...@gmail.com> wrote:
> Hi guys,
> I'm working on the tutorial inwww.djangobook.comand I reached the
> point where I'm making a search form, here is what my view methods
> look like:
>
> def search_form(request):
>     return render_to_response('search_form.html')
>
> def search(request):
>     error = False
>     if 'q' in request.GET and request.GET['q']:
>         q = request.GET['q']
>         if not q:
>             error = True
>         else:
>             books = Book.objects.filter(title__icontains=q)
>             return render_to_response('search_results.html',
> {'books':books, 'query':q})
>     return render_to_response('search_form.html', {'error':error})
>
> the search_form template looks like:
>
> <html>
> <body>
>       <form action="/search" method="get">
>
>            {% if error %}
>                <p style="color:red;"> Please submit a search term </p>
>            {% endif %}
>            Enter the name of the book you're looking for:
>            <br />
>            <br />
>            <input type="text" name="q">
>            <input type="submit" value="Search">
>       </form>
> </body>
> </html>
>
> now, i am supposed to get the error message "Please submit a search
> term" if i submit an empty request. but that is not happening, instead
> i'm getting the ordinary search_form without the error message.
> anybody can give me a hint why?
>
> thanks in advance,
> cheers,
> Lina
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to