On 24 mar, 06:39, Daniel <unagimiy...@gmail.com> wrote: > Hi there, > > I'm reading the django book and they say that you can validate a form > like this in one line, checking for missing keys and missing data: > > if not request.POST.get('subject', ' ') > errors.append('enter a subject')
Are you sure this is the very exact code ? IMHO it's probably something like: if not request.POST.get('subject', ''): errors.append('enter a subject') > I'm not sure how to read that line of code, though. Can someone > explain please? > > Is this checking if the subject key is blank or nil and then appending > the error? Yeps. > But if the subject key is nil, s/nil/missing/ > then the default value for the subject > key is made to be blank (' ')? Would be (and probably is) '', not ' ' (cf above and below). wrt/ your question, it's not clear whether you ask if - in the case of a missing key - .get() will actually update the request.POST object or if it will just return the default instead. But anyway: request.POST is documented has being mostly "a dict-like object" - IOW an object that mostly behave like a regular python dict. If you don't know how a regular python dict behave, then it's time you start learning Python. Else nothing in Django will make sense to you. > How does this code check for a non-existent subject key and the case > that the subject key exists, but is just empty? I give you two clues: 1/ dict.get(<key>[, default=None) returns either the value for <key> or default if <key> is not in dict. 2/ an empty string has a false value in a boolean test. And a general advice: do at least the official Python intro tutorial. You can't expect to make proper use a framework if you don't learn the language it's based on. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.