简体   繁体   中英

$.get() request not working with Django

My $.get() request does not seem to be affecting my Django project.

When you manually change the URL to ?row=0&day=4 for example it works fine, yet when calling the Javascript function index(x) it does not work.

I have checked and the logic works correctly when printing context['entry_form'] the correct html is generated.

I am not sure if I am using the Javascript $get() call correctly?

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)

EDIT: The goal is to get this template code to update correctly:

    <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} );

You're just making a GET request but not doing anything with the response. Ideally, you probably would want to load the returned HTML response to some div or something.

The code should have a 3rd parameter, a callback function executed when the response is returned.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM