I have data that is being submitted from a webpage. I try to re-validate the data on the server side before inserting to DB. For some reason the POST code isn't entirely executed. It reaches the validation with datetime.date() and returns. I am not getting any error or anything.
Screen prints:
New assignment added
Expected screen prints:
New assignment added
Test2
Submit here
The code below is for the view.py:
def addStudent(request, assignment, studentID= -1):
#Check if POST
if request.method == 'POST':
errors = []
print "New assignment added"
#Verify exam date is future and end date is after start
if request.POST['startD'] <= datetime.date():
print "Error"
errors.append("Please verify date is in the future")
print "Test2"
if request.POST['endD'] < request.POST['startD']:
errors.append("Please verify end date>start date.")
print "Submit here"
return
return render(request,.....)
The following is the code that calls it:
$.ajax({type: 'POST',
url:'/assignment/newassignment/{{university}}/',
data:{
csrfmiddlewaretoken:'{{ csrf_token }}',
studentName:$("#studentName").val(),
startD:$("#startDate").val(),
endD:$("#endDate").val()},
async:true});
Disclaimer: The code cannot be a form since visual manipulation is done by the user on the page. Several calls with the ajax are done for each student.
Update : Tried the following debugging:
if request.POST['startD']:
print "date.today():"+str(date.today())
print "request:"+str(request.POST['startD'])
print "myDate:"+str(datetime.strptime(request.POST['startD'], "%Y-%m-%d"))
The first 2 statements are printed and the third isn't
Screen prints:
date.today():2016-03-24
request:2016-03-30
IMPORTANT : from datetime import datetime
I'd imagine you're getting a server error since you're trying to compare values passed across as strings ( startD
/ endD
) to dates. You need to convert them to dates before the comparisons
start = request.POST.get('startD')
if start:
start_date = datetime.strptime(request.POST.get('startD'), "%Y-%m-%d")
if start_date <= datetime.date():
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.