On Thu, 2009-12-31 at 03:20 -0800, BobAalsma wrote:
> Thanks Masklin, I've just tried your suggestion and there is no
> change.
>
> I'm somewhat mystified by the 'u' that gets added to all answer
> messages - is this something to do with unicode? Could this be the
> root of (some) evil?
Again, your're using formatter %r which in python means "representation
of object". In case of strings it prints either u'' for unicode strings,
or just '' for non unicode ones.
It's still a string and doesn't get "appended" magically.
So instead of using %r use %s (which means "string") and you see that
magical u to disappear.
also if you send q (as in example), even it's empty it will get in GET
query dictionary, just because it exists. And it has value - empty
string.
So what you need to test is:
if 'q' in request.GET and request.GET['q']:
Or to make things a bit more Pythonic:
if request.get('q', False):
> I've tried using [ ] instead of the suggested ( ), which leads to a
> Django errorpage, stating that the contents of the variable 'q' is
> u'd' - which seems to indicate to me that a "u" is added to all values
> of q (also explaining why q is never empty).
>
>
> On Dec 31, 11:27 am, Masklinn <[email protected]> wrote:
> > On 31 Dec 2009, at 10:58 , BobAalsma wrote:
> >
> >
> >
> >
> >
> > > I'm following the Django book to learn Django. I'm getting unexpected
> > > responses and can't find how to address this. It seems that submitting
> > > an empty form is not handled as if it is empty ??
> >
> > > In views.py:
> > > def search(request):
> > > if 'q' in request.GET:
> > > message = 'U zocht: %r' % request.GET['q']
> > > else:
> > > message = 'Leeg'
> > > return HttpResponse(message)
> >
> > > However, on submitting an empty form, the displayed message is:
> > > U zocht: u''
> >
> > > On submitting a 'd', the answer is:
> > > U zocht: u'd'
> >
> > > Consistent, that's the good news...
> >
> > > How to solve this?
> >
> > First of all, this has nothing to do with forms. "Forms" in a Django
> > context designates Django
> > Forms:http://docs.djangoproject.com/en/dev/topics/forms/. Here, you're only
> > playing with query parameters, forms are not involved.
> >
> > Second, this is a Python issue more than a Django one: even if the
> > parameter is empty (e.g. `yourpage.com/search?q=`) it is present, therefore
> > it will be put in the GET dictionary. `key in dict` only checks that the
> > key exists, not that it keys to a value (let alone a "falsy" value such as
> > an empty string). What you really want to check is that the 'q' key exists
> > *and is non-empty*.
> >
> > I suggest that you use the `dict.get` method for this: it returns the value
> > for the key if the key exists, None if it doesn't. Just replace `'q' in
> > request.GET` by `request.GET.get('q')` and you should have the behavior you
> > expect.
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.