0

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>
ijames55
  • 186
  • 1
  • 3
  • 15
  • Don't use `{{ request.path }}`, you might or might not have `'django.core.context_processors.request'` in your settings. Use `{% url %}` template tag instead. – Shang Wang Jan 12 '16 at 21:48
  • Okay I have edited my answer, how do I get the template data shown to update? How can I manually change this? – ijames55 Jan 12 '16 at 22:00
  • 1
    http://stackoverflow.com/questions/18976302/returning-rendered-html-via-ajax?answertab=votes#tab-top – Shang Wang Jan 12 '16 at 22:02

1 Answers1

2
$.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);
} );
masnun
  • 11,635
  • 4
  • 39
  • 50
  • I have updated my question. How do I use this response to change the form in my template with / without re rendering the page? – ijames55 Jan 12 '16 at 22:01
  • How do you want to update the form? You can do anything you wish inside the callback. – masnun Jan 12 '16 at 22:09
  • How can I format the response to show as a django form? The code: formattedresponse = {{ response|crispy }} returns nothing. – ijames55 Jan 12 '16 at 22:30
  • 1
    The response is JS so django template code won't work. You have to use JS to do what you want. – masnun Jan 12 '16 at 22:33