$.get() 请求不适用于 Django

$.get() request not working with Django

我的 $.get() 请求似乎没有影响我的 Django 项目。

例如,当您手动将 URL 更改为 ?row=0&day=4 时它工作正常,但是当调用 Javascript 函数 index(x) 时它不起作用。

我已经检查过,当打印上下文 ['entry_form'] 时逻辑工作正常,生成了正确的 html。

我不确定我是否正确使用了 Javascript $get() 调用?

Javascript:

function index(x) {
    theCellIndex = parseInt(x.cellIndex) - 2;
    theRowIndex = parseInt(x.parentNode.rowIndex) - 1;
    $.get( "{{ request.path }}", { 'row': theRowIndex, 'day': theCellIndex} );

}

姜戈:

if "day" and "row" in request.GET:
    day = request.GET.get("day")
    rownum = request.GET.get("row")
    allrows = RowControl.objects.filter(month_control_record=month_control_record)
    row = allrows[int(rownum)]
    selected_date = datetime.date(int(year), int(month), int(day))

    try:
        form_instance = Entry.objects.get(row_control=row, date=selected_date)
        entry_form = EntryForm(instance=form_instance)
    except Entry.DoesNotExist:
        entry_form = EntryForm(initial={'row_control': row, 'date': selected_date})

context = {
    'entry_form': entry_form,
}

print(context['entry_form'])

return render(request, "timesheet/monthview.html", context)

编辑: 目标是让这个模板代码正确更新:

    <form method="POST" action="{{ request.path }}">
    {% csrf_token %}
        {{ entry_form|crispy }}
        <button class="btn btn-primary" type="submit" value="Submit" name="entry_submit" ><i class="fa fa-lg fa-floppy-o"></i> Save</button>
    </form>
$.get( "{{ request.path }}", { 'row': theRowIndex, 'day': theCellIndex} );

您只是发出 GET 请求,但未对响应执行任何操作。理想情况下,您可能希望将返回的 HTML 响应加载到某些 div 或其他内容。

代码应该有第三个参数,一个在返回响应时执行的回调函数。

 $.get( "{{ request.path }}", { 'row': theRowIndex, 'day': theCellIndex}, function(response) {
    console.log(response);
} );