Django的。使用ajaxForm刷新页面的一部分

时间:2017-08-04 20:18:06

标签: javascript jquery html ajax django

我通过django表单提交表单,我希望在页面的某个部分提交数据(div)

 $(function () {
    $('#post-form').ajaxForm({
        success: function (json) {
            console.log(json); // log the returned json to the console
            $('#this').append('snippet.html')
            console.log("success"); // another sanity check
        }
    }) });

的html代码段

<div id="this">

        <tr>
            <th>ID</th>
            <th>Дата</th>
            <th>Цена</th>
            <th>Откуда</th>
            <th>Куда</th>
            <th>Водитель</th>
        </tr>

    {% for item in query_set %}

        <tr>
            <td>{{ item.id }}</td>
            <td>{{ item.date }}</td>
            <td>{{ item.price }}</td>
            <td>{{ item.des_from }}</td>
            <td>{{ item.des_to }}</td>
            <td>{{ item.driver }}</td>
        </tr>

    {% endfor %}
</div>

这是“观看”文件

    def post(self, request):
        csrf_token_value = get_token(self.request)
        data = {}
        form = SchedForm(request.POST)
        if form.is_valid():
            obj = form.save()
            data['result'] = 'Created!'
            data['object'] = {
                'price': obj.price,
                'driver': obj.driver_id,
                'des_from': obj.des_from_id,
                'des_to': obj.des_to_id,
                'date': obj.date,
            }

            return JsonResponse(data)

        else:
            return JsonResponse(form.errors)

问题是我在INTERNET上有一个JsonResponse返回我在网上看到有两个解决方案是通过render_to_response和render_to_string。但就我而言,我需要刷新表单中的数据,你需要render_to_string。我只是没有看到在JsonResponse

的回报中包含它的方法

0 个答案:

没有答案