为什么此错误会出现“意外的EOF”?在我的Django Ajax项目中

时间:2020-10-21 05:55:29

标签: django ajax

我正在尝试使用ajax在其中制作django表,以便我可以在不刷新页面的情况下进行更改。但是我遇到了意外的EOF这样的问题,而且我不知道该怎么办。

and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and

它是base.html文件:

<script type="text/javascript">
        $(document).ready(function() {

            $("#createButton").click(function() {
                var serializedData = $("#createTaskForm").serialize();
                // console.log(serializedData)

                $.ajax({
                    url: $("createTaskForm").data('url'),
                    data: serializedData,
                    type: 'post',
                    success:function(response) {
                        $("#taskList").append('<div class="card mb-1"><div
                        class="card-body">' + response.task.title + '<button type="button" class="close float-
                        right"><span aria-hidden="true">&times;</span></button></
                        div></div>')
                    },
                })

                // $("#createTaskForm")[0].reset();
            });

        });
</script>

它是task_list.html:

{% extends 'task/base.html' %}
{% block title %}
Tasks
{% endblock %}
{% block content %}

<div class="col-8 mt-5">
    <form class="form-inline justify-content-center" id="createTaskForm" method="post" data-url = "{% url 'task_list_url'%}">
        {% csrf_token %}
        {% for field in form %}
        <div class="mx-3">
            {{ field }}
        </div>
        {% endfor %}

        <button type="button" class="btn btn-outline-success" id="createButton">Create</button>
    </form>
</div>
<div class="col-5 mt-5" id="taskList" >
    {% for task in tasks %}
    <div class="card mb-1">
        <div class="card-body">
            {{ task.title }}

            <button type="button" class="close float-right">
            <span aria-hidden="true">&times;</span>
            </button>
        </div>
    </div>
    {% endfor %}
</div>

{% endblock %}

它是views.py文件:

from django.shortcuts import render
from django.shortcuts import redirect

from django.views.generic import View

from django.http import JsonResponse
from django.forms.models import model_to_dict

from .models import Task
from .forms import TaskForm


class TaskList(View):

def get(self,request):
    form = TaskForm()
    tasks = Task.objects.all()
    return render(request, 'task/task_list.html',context = {'form': form,'tasks':tasks})


def post(self,request):
    form = TaskForm(request.POST)

    if form.is_valid():
        new_task = form.save()
        return JsonResponse({'task': model_to_dict(new_task)},status = 200)
    else:
        return reditect('task_list_url')

那么,什么问题呢?

0 个答案:

没有答案