簡體   English   中英

$ .get()請求不適用於Django

[英]$.get() request not working with Django

我的$ .get()請求似乎並未影響我的Django項目。

例如,當您手動將URL更改為?row = 0&day = 4時,它可以正常工作,但是在調用Javascript函數index(x)時,它不起作用。

我已經檢查過,並且在打印context ['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} );

}

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>
$.get( "{{ request.path }}", { 'row': theRowIndex, 'day': theCellIndex} );

您只是在發出GET請求,而對響應不做任何事情。 理想情況下,您可能希望將返回的HTML響應加載到某個div或其他內容。

該代碼應具有第3個參數,即返回響應時執行的回調函數。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM