request.GET.get() returns None 一直 - django

request.GET.get() returns None all the time - django

我有这两个型号:

class Payment(models.Model):
    admin = models.ForeignKey(User,on_delete=models.PROTECT)
    client_seller = models.ForeignKey(ClientCompany,on_delete=models.PROTECT,blank=True)
    next_payment = models.OneToOneField(NextPayment,blank=True,null=True,related_name='next_payments',on_delete=models.PROTECT)
    #others

class NextPayment(models.Model):
    next_payment = models.DateTimeField()
    status = models.BooleanField(default=True)

我想在创建 Payment 实例之前创建 NextPayment 实例,并将 NextPayment 对象分配给 Payment > next_payment 字段!这是我的 views.py

@login_required
def create_payment(request):
    main_form = PaymentForm()
    next_paymentform = NextPaymentForm()
    next_payment= request.GET.get('next_payment')
    print(next_payment)
    if request.method == 'POST' and request.is_ajax():
        main_form = PaymentForm(request.POST)
        next_paymentform = NextPaymentForm(request.POST)
        if main_form.is_valid():
            main_obj = main_form.save(commit=False)
            main_obj.admin = request.user
            if next_payment:
                date_next = next_paymentform(next_payment=next_payment)
                #date_next = NextPayment(next_payment=next_payment,status=True) also tried this
                date_next.save()
                main_obj.next_payment= date_next
                main_obj.save()
            else:
                main_obj.save()
            data = {
                 'id':main_obj.id
            }
            return JsonResponse({'success':True,'data':data})
        else:
            return JsonResponse({'success':False,'error_msg':main_form.errors,'next_paymentform':next_paymentform.errors})
    return render(request,'payments/pay.html',{'main_form':main_form,'next_paymentform':next_paymentform})

我的forms.py

class NextPaymentForm(forms.ModelForm):
    next_payment = forms.DateTimeField(input_formats=['%Y-%m-%dT%H:%M'],widget=forms.DateTimeInput(attrs={'type':'datetime-local','class':'form-control'}),required=False)    
    class Meta:
        model = NextPayment
        fields = ['next_payment']

class PaymentForm(forms.ModelForm):
    class Meta:
        model = Payment
        fields = ['client_seller','type_of_payment','price','note']

这是我的模板

            <form action="" method="POST" id="create-payment-form">{% csrf_token %}        

            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                    <i class="fas fa-user-check"></i>
                    <label>name </label>
                    {{ main_form.client_seller | add_class:'form-control' | append_attr:'onchange:currentPaymentBalance();' }}
                    </div>
                </div>
                <!-- /.col -->
                <div class="col-md-6">
                    <div class="form-group">
                    <i class="fas fa-money-check-alt"></i>
                    <label>number</label>
                    <input type="number" class="form-control" disabled id="balance_client">
                    </div>

                    <!-- /.form-group -->
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                    <i class="far fa-handshake"></i>
                    <label> type</label>
                    {{ main_form.type_of_payment | add_class:'form-control' }}
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                    <i class="fas fa-money-check-edit-alt"></i>
                    <label> price</label>
                    {{ main_form.price }}
                    </div>
                </div>    
                <div class="col-md-12 col-12">
                    <div class="form-group text-center ">
                    <i class="far fa-comment-dots"></i>
                    <label class="">note</label> 
                    {{ main_form.note | add_class:'form-control' }}
                    </div>
                </div>

                    <div class="col-md-6 justify-content-center">
                        <div class="form-group">
                        <i class="fas fa-quote-right"></i>
                        <label> next payment</label>
                        {{ next_paymentform.next_payment }}
                        </div>
                    </div>                                        
    
            <!-- /.col -->
            </div>
            <!-- /.row -->
            <div class="card-footer">
                <div class="row justify-content-center">
                    <button type="submit" class="btn btn-lg btn-success">زیادکردن</button>
                </div>
            </div>
        </form>

next_payment 输出 None!但它不是 None 但不起作用,只保存主窗体!我需要检查 next_payment 是否存在然后创建 NextPayment 的实例并将该新实例分配给 Payment 模型? 非常感谢任何想法。
提前谢谢你...

由于您的表单正在发送 POST 请求,您必须更改

next_payment= request.GET.get('next_payment')

next_payment= request.POST.get('next_payment')