Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Check if request.GET var is None?

Tags:

python

django

I'm getting into django and this is getting me a headache. I'm trying to get a simple GET variable. URL is site.com/search/?q=search-term

My view is:

def search(request):     if request.method == 'GET' and 'q' in request.GET:         q = request.GET.get('q', None)         if q is not None:             results = Task.objects.filter(                                    Q(title__contains=q)                                    |                                    Q(description__contains=q),                                    )             ...return...         else:             ...     else:         ... 

Search queries like mysite.com/search/? (without q) get through the first if correctly.

The problem is in queries like mysite.com/search/?q=. They don't get caught by if q is not None:

So, the short answer would be How can I check q == None? (I've already tried '', None, etc, to no avail.)

like image 351
Nacho Avatar asked Mar 11 '10 02:03

Nacho


People also ask

What is returned by the request GET GET?

request. GET contains the GET variables. These are what you see in your browser's address bar.

What is HttpResponse in Django?

HttpResponse Methods – DjangoIt is used to set the given header name to the given value. HttpResponse.__delitem__(header) It deletes the header with the given name. HttpResponse.__getitem__(header) It returns the value for the given header name.

What does a request get return in Django?

When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function. Each view is responsible for returning an HttpResponse object.

How does get work in Django?

GET is an HTTP method in Django that encapsulates the data in a string and utilizes it to construct a URL. The URL includes the data keys and values as well as the address to which the data must be transmitted.


2 Answers

First, check if the request.GET dict contains a parameter named q. You're doing this properly already:

if request.method == 'GET' and 'q' in request.GET: 

Next, check if the value of q is either None or the empty string. To do that, you can write this:

q = request.GET['q'] if q is not None and q != '':     # Do processing here 

Notice that it is not necessary to write request.GET.get('q', None). We've already checked to make sure there is a 'q' key inside the request.GET dict, so we can grab the value directly. The only time you should use the get method is if you're not sure a dict has a certain key and want to avoid raising a KeyError exception.

However, there is an even better solution based on the following facts:

  • The value None evaluates to False
  • The empty string '' also evaluates to False
  • Any non-empty string evaluates to True.

So now you can write:

q = request.GET['q'] if q:     # Do processing here 

See these other resources for more details:

  • Python: Truth Value Testing
  • Python: dict.get
like image 54
Wesley Avatar answered Sep 30 '22 19:09

Wesley


Thanks for the clarification by @Ned. Found a complete explanation here.

Basically:

'==' can be thought of as "value equality", that is, if two things look the same, == should return a true value. (For those with a Java background, Python's == is actually doing something akin to an equals() method.)

'is' can be thought of as 'object identity', that is, if the two things actually are the same object.

like image 45
Nacho Avatar answered Sep 30 '22 19:09

Nacho