Sunday, September 18, 2016

Date field struggles

Things have been busy and I have not been able to be at my computer lately to work on programming. Looking at my calendar though it feels longer than it actually is as it was only last week I made a fulfilling breakthrough. Although I can't say I still completely understand the date field I feel comfortable using it.

The first trouble I had is that in the date field from Flask forms. I knew how to display that part of the form but when I would enter a date, manually, into the field, upon submission the page would display "Not a valid date" above the field. I actually just commented that code out for a few weeks and worked on other areas of the site. That proved very successful as I basically completed the app in that time and only had the elusive date field to tackle. By that time I was already confident in the app and the date shouldn't be too difficult to handle right?

As it stood the form was not recognizing the data in the field as a valid date. I thought maybe having a date picker would somehow enter correct information. So I searched and found jQuery ui. That would allow me to attach a date picker to an element so that when clicked it would show a date picker. It worked to an extent in that the page would display a date picker widget when clicking the date field but still said it was not valid data.

Many Google and StackOverflow searches later I came to the conclusion the backend was not parsing the data the way it should have been. When sending the data from the form, using the data in the app must be implemented using:

date.strftime(date_variable, "string format") - string format is for the date, i.e. '%Y-%m-%d'

So when sending the data from the web to the back end the data can be entered into an SQL query where the column is for dates.

So what I learned is the Flask date field must convert the entered string into a unix time object because you must enter the date format as a parameter when creating the date field. That data then must be sent to Python script and entered into the database. However date.strfime must be used so the format can be parsed by SQL as a date object and acceptably entered into the database. For a few tries there was a date entered but it was '0000-00-00'. Obviously not what I wanted.

So I suppose I do have an idea of why things were not functioning as I wanted them to. I was so happy to finally figure out the bane of my last few weeks and be so close to an MVP at last. One more roadblock though.

Right now I am having difficulty with multiple browsers. Most specifically Chrome and Safari (I haven't checked Firefox yet though I suppose I should). The trouble is the date picker widget created with jQuery ui. It functions just fine in Safari and technically works in Chrome. The problem arises in the fact that Chrome has its own date picker built in. Thus I get the error of not valid data when using the date picker. If I don't attach the jQuery date picker to the date field, Chrome's date picker will work fine and the data can be entered. The problem is using Safari then, the date picker cannot be used. Catch 22?

So I have made great strides, in my opinion, in understanding datetimes. But browser compatibility is a new challenge. I suppose that may never be solved even by the best developers.

25 words or less: Understanding how to parse datetimes is an invaluable tool and should not be overlooked. Keep trying, you'll get it.

Also I can't say my lack of posts is not entirely due to a recent obsession with Mad Men, but it certainly had a larger impact on my nonproductivity. Maybe someday I'll deviate some and write a post on my view of the show and understanding the ending.

No comments:

Post a Comment