You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django接口无法通过.get()传递多个查询参数问题求助

Troubleshooting Parameter Passing in Your Django API Request

Hey there, let's work through this frustrating parameter-passing issue you're dealing with—50+ attempts sound brutal, so let's break this down step by step.

First up, the checkbox state mistake you mentioned: using $(this).checked is likely part of the problem. That's not the right way to get a checkbox's checked state with jQuery. Here's what you should use instead:

  • Native JS: this.checked (returns a boolean: true/false)
  • jQuery: $(this).is(':checked') (also returns a boolean)

If you were using $(this).checked, it was probably returning undefined, which would mess up your unread parameter and cause the request to fail unless you hardcoded it to "1".

Next, let's fix how you're passing both suggestion_id and unread to your Django endpoint. Instead of manually building the URL (which can lead to typos or missing parameters), use jQuery's $.ajax with the data property—it handles parameter encoding and formatting automatically.

Example Frontend Code (jQuery)

// Attach this to your checkbox's change event
$('.suggestion-checkbox').on('change', function() {
    // Grab the suggestion_id (adjust this to match how you store the ID in your HTML)
    const suggestionId = $(this).closest('.suggestion-item').data('suggestion-id');
    // Get the correct checked state, convert to 1/0 for your API
    const unreadState = $(this).is(':checked') ? 1 : 0;

    // Send the request
    $.ajax({
        url: '/your-django-endpoint/', // Replace with your actual endpoint URL
        method: 'GET',
        data: {
            suggestion_id: suggestionId,
            unread: unreadState
        },
        success: function(response) {
            console.log('Request succeeded!', response);
        },
        error: function(xhr) {
            console.error('Request failed:', xhr.responseText);
        }
    });
});

Example Django View Code

Make sure your backend is correctly pulling the parameters from the GET request:

from django.http import JsonResponse

def update_suggestion_status(request):
    # Get parameters from the GET request (use .get() to avoid KeyError if missing)
    suggestion_id = request.GET.get('suggestion_id')
    unread = request.GET.get('unread')

    # Validate that both parameters exist
    if not suggestion_id or unread is None:
        return JsonResponse({'error': 'Missing required parameters'}, status=400)

    # Your business logic here (e.g., update a database record)
    # ...

    return JsonResponse({'status': 'success', 'message': 'Status updated'})

Quick Debugging Tips

  • Check the Network Tab: Open your browser's dev tools (F12), go to the Network tab, and look at the actual GET request being sent. You'll see the full URL with parameters—verify that both suggestion_id and unread are present and have the correct values.
  • Verify Parameter Names: Double-check that the parameter names in your frontend (suggestion_id, unread) exactly match what your Django view is expecting (case-sensitive!).
  • Test with Hardcoded Values First: Once you have the code above, try hardcoding both parameters temporarily to confirm the backend works, then switch back to dynamic values.

Since you got it working when hardcoding unread to "1", the backend is likely set up correctly—this is almost certainly a frontend issue with how you're capturing the checkbox state or formatting the request.

内容的提问来源于stack exchange,提问作者rschwieb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:16:05