简体   繁体   English

Ajax django POST请求提前结束

[英]Ajax django POST requests ends prematuraly

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. 我尝试在插入DB之前在服务器端重新验证数据。 For some reason the POST code isn't entirely executed. 由于某种原因,POST代码没有完全执行。 It reaches the validation with datetime.date() and returns. 它通过datetime.date()到达验证并返回。 I am not getting any error or anything. 我没有收到任何错误或任何东西。

Screen prints: 屏幕打印:

New assignment added 添加了新作业

Expected screen prints: 预期的屏幕打印:

New assignment added 添加了新作业

Test2 测试2

Submit here 在这里提交

The code below is for the view.py: 以下代码用于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. 每个学生都要用ajax进行几次调用。

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 前2条语句已打印,而第三条则没有

Screen prints: 屏幕打印:

date.today():2016-03-24 date.today():2016-03-24

request:2016-03-30 要求:2016-03-30

IMPORTANT : from datetime import datetime 重要提示 :from datetime导入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. 我想您会收到服务器错误,因为您试图将通过字符串( startD / endD )传递的值与日期进行比较。 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():

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM