Ajax不显示元素

时间:2017-03-26 23:21:44

标签: jquery python ajax django

我没什么问题。在我的Django项目中,我使用模态窗口创建,然后使用Ajax更新对象列表。您可以在下面看到代码和{%if request_user_is_business_analyst%}内的两个链接在更新后消失。他们为什么变得隐形?以及如何解决这个问题?

group_tasks_list.html

{% for group_task in group_tasks %}
    <tr>
        <td>{{ group_task.name }}</td>
        <td>
            <a class="btn btn-info text-center" href="{{ group_task.get_absolute_url }}">{% trans 'OPEN' %}</a>
            {% if request_user_is_business_analyst %}
                <a class="btn btn-warning text-center" href="{{ group_task.get_absolute_url }}"><i class="fa fa-pencil" aria-hidden="true"></i></a>
                <a class="btn btn-danger text-center" href="{{ group_task.get_absolute_url }}"><i class="fa fa-trash-o" aria-hidden="true"></i></a>
            {% endif %}
        </td>
    </tr>
{% empty %}
    <tr>
        <td colspan="3" class="table-danger text-center"><i class="fa fa-info-circle" aria-hidden="true"></i> {% trans 'NO DATA' %}</td>
    </tr>
{% endfor %}

view.py

def group_task_add(request, project_code):
    data = dict()
    project = get_object_or_404(Project, pk=project_code)
    if request.method == 'POST':
        form = GroupTaskAddForm(request.POST)
        if form.is_valid():
            group_task = form.save(commit=False)
            group_task.project = project
            group_task.save()
            data['form_is_valid'] = True
            group_tasks = GroupTask.objects.filter(project=project_code)
            data['html_group_tasks'] = render_to_string('project/group_tasks_list.html', {'group_tasks': group_tasks})
        else:
            data['form_is_valid'] = False
    else:
        form = GroupTaskAddForm()
    context = {'project': project, 'form': form}
    data['html_group_task_add_form'] = render_to_string('project/group_task_add.html', context, request=request)
    return JsonResponse(data)

def project_detail(request, project_code):
    project = get_object_or_404(Project, pk=project_code, status='open')
    group_tasks = GroupTask.objects.filter(project=project_code)
    request_user_is_business_analyst = project.member_set.filter(user=request.user, role='business_analyst').exists()
    context = {
        'project': project,
        'group_tasks': group_tasks,
        'request_user_is_business_analyst': request_user_is_business_analyst,
    }
    return render(request, 'project/project_detail.html', context)

project_detail.html

****
{% include 'project/group_tasks_list.html' %}
****

JS

$(function () {
            var loadForm = function () {
                var btn = $(this);
                $.ajax({
                    url: btn.attr("data-url"),
                    type: 'get',
                    dataType: 'json',
                    beforeSend: function () {
                        $("#add-group-task-modal").modal("show");
                    },
                    success: function (data) {
                        $("#add-group-task-modal .modal-content").html(data.html_group_task_add_form);
                    }
                });
            };

            var saveForm = function () {
                var form = $(this);
                $.ajax({
                    url: form.attr("action"),
                    data: form.serialize(),
                    type: form.attr("method"),
                    dataType: 'json',
                    success: function (data) {
                        if (data.form_is_valid) {
                            $("#group-tasks-table tbody").html(data.html_group_tasks);
                            $("#add-group-task-modal").modal("hide");
                        }
                        else {
                            $("#add-group-task-modal .modal-content").html(data.html_group_task_add_form);
                        }
                    }
                });
                return false;
            };
            // Create book
            $(".add-group-task").click(loadForm);
            $("#add-group-task-modal").on("submit", ".js-group-task-add-form", saveForm);
        });

0 个答案:

没有答案