Django form query

Go To StackoverFlow.com

0

I saw this piece of code and have few questions..If anyone could explain that would be really helpful.

views.py

def search_page(request):
    pdb.set_trace()
    form = SearchForm()
    bookmarks = []  
    show_results = False
    if 'query' in request.GET:
        show_results = True
        query = request.GET['query'].strip()
        if query:
           form = SearchForm({'query': query})
           bookmarks = Bookmark.objects.filter(title__icontains=query)
    variables = RequestContext(request,{'form': form,
                                       'bookmarks': bookmarks,
                       'show_results': show_results,
                       'show_tags': True,
                       'show_user': True})

    return render_t7tr o_response('search.html', variables)

form.py class SearchForm(forms.Form): query = forms.CharField(label=u'Enter a keyword to search for', widget=forms.TextInput(attrs={'size':32}))

How do the below line of code work?

If 'query' in request.GET

how the 'query' string is in the request.Get?..When I debugged the dictionary contains the value contains the search value I have given.

THe code works fine but I want to understand.

2012-04-04 02:27
by user1050619


0

Containment testing on mappings checks the keys.

key in d

Return True if d has a key key, else False.

EDIT:

Django parses the query string and populates request.GET from it.

2012-04-04 02:29
by Ignacio Vazquez-Abrams
how does the dictionary contains the KEY-'query'..The VALUE that I gave is 'GOOGLE'(Example). - user1050619 2012-04-04 02:33
Ads