Tuesday, January 30, 2018

Re: how do I handle a dropdown in Django whose only purpose is to get data.

On vrijdag 26 januari 2018 22:25:55 CET eileen@themaii.org wrote:

> well, you certainly got me correct! I'm fairly new to python - didn't do

> much coding to learn. Trial by fire, sort of. and the original person who

> put this together is now gone from the company and won't answer any

> outstanding questions. Plus, I *think* he got most of the code from 3rd

> party sources. I could be wrong on that. I was raised on Perl and PL/I (as

> ancient as the latter may seem)

 

I meant to follow this up, didn't have the opportunity till now.

 

I strongly suggest to get the python basics down. Your Perl hash is your python dict, array is a list (tuple for "read only" lists) and you have you basic scalars with str, bool, int and float. And you have objects. In fact, everything, including scalars, class definitions, methods and functions are objects.

 

Standard comparison is "==". The "is" comparison is stricter and only used for boolean values and the special value None (also handy to know is that functions / methods that do not return anything actually return None).

 

One can think of "is" comparison as "equal to and of the same type":

 

>>> empty = ''

>>> empty is None

False

>>> empty is False

False

>>> empty is True

False

>>> bool(empty)

False

>>> bool(empty) is False

True

>>> bool(None)

False

 

If you want a quick intro to Python, I highly suggest Python Tips.


Finally, it's important to know Django
project layout and in your case, getting a handle on views and querysets.

 

>

> On Friday, January 26, 2018 at 3:12:19 PM UTC-5, Melvyn Sopacua wrote:

> > There are a bunch of issues with this code:

> >

> > 1) Spell Physical correctly:

> > > 2 - Phyiscal

> > >

> > > if form.data['handicapped'] is 'Physical' or 'Mental':

> > > handicapped = forms.ChoiceField(choices=[(x, x) for x in ('-------',

> > >

> > > 'Mental', 'Physcal')], required=False)

> >

> > Comparison of non boolean or None values using "is" instead of "==":

> > > if form.data['handicapped'] is 'Physical' or 'Mental':

> > Use of or as if natural language in RHS of comparisons (should be a == x

> > or a

> >

> > == y, not a == x or y):

> > > if form.data['handicapped'] is 'Physical' or 'Mental':

> >

> > > if form['handicapped'].data == 1 or 2:

> > So this reads as if someone with very little knowledge of Python is

> > modifying

> > a 3rd party app, written by someone with a decent amount of Django

> > experience.

> >

> > Finally, when accessing form data from a valid form, we access

> > form.cleaned_data and generally use a local variable to reference the

> > dict:

> >

> > if form.is_valid():

> > data = form.cleaned_data

> > # do stuff with data

 

 

--

Melvyn Sopacua

No comments:

Post a Comment