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.
Containment testing on mappings checks the keys.
key in d
Return
True
if d has a key key, elseFalse
.
EDIT:
Django parses the query string and populates request.GET
from it.