52👍
Calling /search/
should result in “you submitted nothing”, but calling /search/?q=
on the other hand should result in “you submitted u””
Browsers have to add the q=
even when it’s empty, because they have to include all fields which are part of the form. Only if you do some DOM manipulation in Javascript (or a custom javascript submit action), you might get such a behavior, but only if the user has javascript enabled. So you should probably simply test for non-empty strings, e.g:
if request.GET.get('q'):
message = 'You submitted: %r' % request.GET['q']
else:
message = 'You submitted nothing!'
- [Django]-Catching DoesNotExist exception in a custom manager in Django
- [Django]-How do I add a Foreign Key Field to a ModelForm in Django?
- [Django]-Django Rest Framework remove csrf
2👍
since your form has a field called ‘q’, leaving it blank still sends an empty string.
try
if 'q' in request.GET and request.GET['q'] != "" :
message
else
error message
- [Django]-Python coverage badges, how to get them?
- [Django]-How to get the current language in Django?
- [Django]-How can I get the full/absolute URL (with domain) in Django?
2👍
In python, None, 0, “”(empty string), False are all accepted None.
So:
if request.GET['q']: // true if q contains anything but not ""
message
else : //// since this returns "" ant this is equals to None
error
- [Django]-In a django web application, how do you give users their own subdomain?
- [Django]-Why does django 1.7 creates migrations for changes in field choices?
- [Django]-How to get Request.User in Django-Rest-Framework serializer?
1👍
Here is a good way to do it.
from django.utils.datastructures import MultiValueDictKeyError
try:
message = 'You submitted: %r' % request.GET['q']
except MultiValueDictKeyError:
message = 'You submitted nothing!'
You don’t need to check again if q is in GET request. The call in the QueryDict.get already does that to you.
- [Django]-Having trouble with user.is_authenticated in django template
- [Django]-How to run Django's test database only in memory?
- [Django]-How to use if/else condition on Django Templates?
1👍
from django.http import QueryDict
def search(request):
if request.GET.\__contains__("q"):
message = 'You submitted: %r' % request.GET['q']
else:
message = 'You submitted nothing!'
return HttpResponse(message)
Use this way, django offical document recommended __contains__ method. See https://docs.djangoproject.com/en/1.9/ref/request-response/
- [Django]-Django testing: Test the initial value of a form field
- [Django]-Django model fields getter / setter
- [Django]-When should you use django-admin.py versus manage.py?
0👍
def search(request):
if 'q' in request.GET.keys():
message = 'You submitted: %r' % request.GET['q']
else:
message = 'You submitted nothing!'
return HttpResponse(message)
you can use if … in too.
- [Django]-Foreign key from one app into another in Django
- [Django]-How do you reload a Django model module using the interactive interpreter via "manage.py shell"?
- [Django]-Django REST Framework viewset per-action permissions
0👍
msg = request.GET.get('q','default')
if (msg == default):
message = "YOU SUBMITTED NOTHING"
else:
message = "you submitted = %s" %msg"
return HttpResponse(message);
- [Django]-How do you detect a new instance of the model in Django's model.save()
- [Django]-VSCode terminal shows incorrect python version and path, launching terminal from anaconda works perfectly
- [Django]-Adding custom fields to users in Django