【问题标题】:django: problem with ajax request sent to ModelFormset templatedjango:发送到 ModelFormset 模板的 ajax 请求有问题
【发布时间】:2021-02-14 12:01:57
【问题描述】:

我正在尝试将用户数据从一个模板传递到另一个模板中。为此,我使用了一个 ajax 请求,这里也有解释 How do I integrate Ajax with Django applications?

虽然没有出现错误,但没有任何内容被拉取。

这是我的模型表单集视图在模板 1 中的样子:

def New_Sales(request):
    #context = {}
    form = modelformset_factory(historical_recent_data, fields=('id','Id', 'Date','Quantity', 'NetAmount', 'customer_name'))
    
    
    if request.method == 'GET':
        formset = form(queryset= historical_recent_data.objects.none())
        #blank_form = formset.empty_form
    elif request.method == 'POST':
        formset = form(request.POST)
        #blank_form = formset.empty_form
        if formset.is_valid():
            request.session['sale'] = request.POST.get('sale')
            
            
            
            
            
            for check_form in formset:
                check_form.save()
            
                quantity = check_form.cleaned_data.get('Quantity')
                id = check_form.cleaned_data.get('Id')
                update = replenishment.objects.filter(Id = id).update(StockOnHand = F('StockOnHand') - quantity)
                update2 = Item2.objects.filter(reference = id).update(stock_reel = F('stock_reel') - quantity)
                
            
            
        
            return redirect('/invoice/pdf/assembly/') 
    
    #else:
        #form = form(queryset= historical_recent_data.objects.none())
        
    
    
    return render(request, 'new_sale.html', {'formset':formset})

这是将模板 1 数据访问到模板 2 的视图:

def generate_pdf_assembly(request):
    my_company = MyCompany.objects.get(id = 1)
    
    request = request.session.get('sale')
    context = {'request' : request, 'my_company' : my_company }
    print(context)

这是从模板(在模板 2 中)访问数据的 ajax 请求:

<h3> {{ context }} </h3>

<script>
        
            $.ajax({
                method: "GET",
                url: "/new_sale.html", 
                sucess: function(context){
                    alert(context);
                },
                failure: function(context){
                    alert('got an error');
                    
                }
            });
            
        
    </script>

我觉得视图中的request.session 肯定有问题,因为在日志和 Chrome 控制台中都没有输出明显的错误,但我现在没有能力进一步调试它。

更新:在标签模板中将context 更改为request 后,出现None 值,这肯定是请求的问题

【问题讨论】:

    标签: python django ajax


    【解决方案1】:
    def username_exists(request):
    data = {'msg':''}   
    if request.method == 'GET':
        username = request.GET.get('username').lower()
        exists = Usernames.objects.filter(name=username).exists()
        if exists:
            data['msg'] = username + ' already exists.'
        else:
            data['msg'] = username + ' does not exists.'`enter code here`
    return JsonResponse(data)
    

    【讨论】:

    • alif 感谢您的回复,您能告诉我更多关于这对我有什么帮助吗?
    • 嗨,欢迎来到 Stack Overflow。虽然这可能会回答问题,但最好包括答案的基本部分,例如此代码的作用以及应如何使用。
    猜你喜欢
    • 2014-05-14
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多