J
J
joli2019-03-13 21:55:21
Django
joli, 2019-03-13 21:55:21

Why is the query condition written in quotation marks?

I'm learning django from a book, I got to the site search.
Can you explain why (if 'query' in request.GET) query is written in quotes?

#forms.py
class SearchForm(forms.Form):
    query = forms.CharField()

#views.py
def post_search(request):
    form = SearchForm()
    query = None
    results = []
    if 'query' in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            query = form.cleaned_data['query']
            results = Post.objects.annotate(search=SearchForm('title', 'body')).filter(search=query)
    return render(requets, 'blog/post/search.html', {'form': form,
                                                     'query': query,
                                                     'results': results})

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2019-03-13
@joli

because type == str
If it was not passed in the parameters, there is no need to load the server

V
Vadim Shatalov, 2019-03-13
@netpastor

Learn python first, and then already Django
request.GET is a dictionary and so it is searched in it for an entry with such a key

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question