Reputation: 3814
EDIT: My def form_invalid of the `class ProviderUpdateView(UpdateView):
def form_invalid(self, form):
print("form is invalid")
print(form.errors)
return HttpResponse("form is invalid.. this is just an HttpResponse object")
I might open a second question to figure out why this isn't rendering on the form and yet some 'must fill out this field is' might have to do with crispy forms not sure I didn't write this part and have to look into it :/
I have a date field that comes across in the data field of an ajax call. I can get the data just fine in my views.py
dateJ = request.GET.get('date_joined_group')
This is great, but when I try to throw it into my model it dies everytime. I need to 'clean' the data, I know my forms when I am just doing standard get post ops works and does this automagically. Is there a way I can invoke this automagic when not using form stuff but using the ajax call?
I tried this:
dateObj = datetime.datetime.strptime(str(dateJ), '%m-%d-%Y')
Then in my model:
grpProvInfo.date_joined_group = dateObj
grpProvInfo.save()
This worked, till I used the date picker on the form which put it in as 4/29/2009
So the slashes broke my 'slick fix', gotta be an easier way then trying to account for all the possibilities just not sure how to invoke it from a little def method I have for the ajax call in my python.
Upvotes: 1
Views: 456
Reputation: 899
So there are a few different python libraries that can help you in a big way. This one, dateparser, lets you input as many different formats as you'd like to be able to parse, and returns a datetime object. This would solve your problem with slashes. Another piece of advice would be to always serve your date in the same format regardless of how it is input. If you use django forms and the date field, you will automatically get validation (of a valid date string), and that combined with dateparser should allow you to safely parse any date string.
And for simplicity's sake, you should be able to change the default format of the datepicker. But that's not truly the answer for how to fix your problem
Upvotes: 2