避免Django

时间:2018-03-19 05:41:31

标签: django python-3.x transactions django-forms

我在表单中使用自动提交。

在数字字段中输入10位数字时,表格会自动提交。这实际上来自USB RFID读卡器扫描卡(它作为键盘模拟器)。

我将数字与数据库中的现有数字进行比较,然后识别用户并执行一些数据库事务。

读卡器会多次读取该号码,并且由于在该输入完成所有数据库事务之前,在1秒内接受另一个输入(相同的号码)并且在第一个交易之前丢失一致性完全完成。

请告知。

VIEWS.PY档案:

@login_required
def markattdview(request):
    # for val in studmst.objects.filter(ccownerid=request.user.id)
# TO DOooo
    # select studid from attddetail where studentID= (select student ID from studmst where ccownerid = currentuser.ID)
    # lastattd=attddetail.objects.filter()
    # Showing last 10 users whose attendance have been marked by the logged in user
    #
    form_attd = markattdform()



    attdsuccess = ""
    identified_user = ""
    identified_studname=""
    loggedin_userid=request.user.id
    loggedinuser=request.user.username
    print("Logged in User : " + loggedinuser)

    if request.method == 'POST':
         studmstobj = studentmaster()

         attdform = markattdform(request.POST)
         unknown_cardno = request.POST.get('cardno')

         if attdform.is_valid():

                    #  form_attd.punchtime = datetime.datetime.now()
                    #  attdform.save(commit=True)


              obj = attdform.save(commit=False)
              obj.punchtime = datetime.datetime.now()
              attdsuccess = "Card not assigned. Kindly verify if the card has been registered."

              #studlist = studmst.objects.filter(ccownerid = request.user.id)
              #print (studlist[0])

#              for usr in studlist(cardnumber = obj.cardno):
# Identifying the user who swyped the card

              for usr in studentmaster.objects.filter(cardnumber = obj.cardno):
#WORKING FINE AFTER REMOVING WORKER AND REDISTOGO
#saving the details for identified User
                    identified_studname= usr.studentname
                    identified_cardno=usr.cardnumber
                    identified_studid = usr.studentid
                    punchdatetime=obj.punchtime
                    obj.studentname=identified_studname
                    obj.studentid_id=identified_studid
                    print('User Identified for card number  '+ identified_cardno)
                    print( "Identified user - " + identified_studname)
                    databasetransactions(identified_studid,identified_studname,punchdatetime)

自动表格提交的JAVASCRIPT:

$(function () {
  console.log(thetext);
    $('#thetext').bind('change keyup', function () {
        if ($(this).val().length >= 10) {
            $('#Form').submit();

        }
    })

});

1 个答案:

答案 0 :(得分:0)

我会添加两个变量:formSubmittedpreviousInput。并且每当文本的长度> 10,我会检查表单是否已提交,以及先前的输入是否等于当前输入。

提交表单时,您将formSubmitted的值设置为true

在这种情况下,如果表单尚未提交,或者如果之前的输入与当前输入不同,这意味着输入了另一个文本并且还应该提交,那么您的表单将被提交。

最后在ajax成功或错误上,你将formSubmited设置为false以允许新的表单子项。

请记住,如果当前输入与前一个输入不同,您的表单也会被提交!

$(function () {
    var formSubmitted = false;
    var previousInput = null;
    $('#thetext').bind('change keyup', function () {
        if ($(this).val().length >= 10 && (!formSubmitted || previousInput != $(this).val())) {
            var formData = new FormData($('#Form')[0]);

            previousInput = $(this).val();
            formSubmitted = true;
            $.ajax(
                url: '/endpoint.php',
                method: 'POST',
                processData: false,
                contentType: false,
                data: formData,
                success: function(data, status, xhr) {
                    formSubmitted = false;
                },
                error: function(data, status, xhr) {
                    formSubmitted = false;
                }
           });

        }
    })
});
相关问题