$.get()请求不能与Djang一起使用

2024-04-24 13:46:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我已经检查了,并且在打印上下文['entry\u 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} );

}

Django公司:

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>

Tags: instanceformgetdaterequestcontextjavascriptcontrol
1条回答
网友
1楼 · 发布于 2024-04-24 13:46:07
$.get( "{{ request.path }}", { 'row': theRowIndex, 'day': theCellIndex} );

您只是发出了一个GET请求,但没有对响应执行任何操作。理想情况下,您可能希望将返回的HTML响应加载到某个div或其他地方。你知道吗

代码应该有第三个参数,即返回响应时执行的回调函数。你知道吗

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

相关问题 更多 >