AJAX 不更新页面

AJAX dont update page

我的项目有奇怪的问题。谁能说说为什么会这样以及如何解决。

我有 edit/update 对象 (function_edit) 的功能。我还使用 AJAX 在成功编辑后更新对象列表 (function_list),并且我还使用 django-reversion 应用程序来控制每个对象的更改。 function_list 有部分仅对具有特殊角色 (request_user_is_business_analyst) 的用户可见。当我在视图中使用 request_user_is_business_analyst 时,它会引发错误,而当我不使用它时,视图功能工作正常。另外,当我不使用 django-reversion request_user_is_business_analyst 时效果很好。

urs.py:

url(r'^(?P<project_code>[0-9a-f-]+)/(?P<function_code>[0-9a-f-]+)/edit/$',
    function_edit,
    name='function_edit'),

views.py:

@reversion.create_revision()
def function_edit(request, project_code, function_code):
    data = dict()
    project = get_object_or_404(Project, pk=project_code)
    request_user_is_business_analyst = project.member_set.filter(user=request.user, role='business_analyst').exists()
    function = get_object_or_404(Function, pk=function_code)
    if request.method == 'POST':
        form = FunctionAddForm(request.POST, instance=function)
        if form.is_valid():
            form.save()
            data['form_is_valid'] = True
            functions = Function.objects.filter(project=project_code)
            data['html_function'] = render_to_string('project/function_list.html', {'functions': functions, 'request_user_is_business_analyst': request_user_is_business_analyst})
            # Store some meta-information for reversion.
            reversion.set_user(request.user)
            reversion.set_comment('EDIT')
        else:
            data['form_is_valid'] = False
    else:
        form = FunctionAddForm(instance=function)
    context = {'project': project, 'function': function, 'form': form}
    data['html_function_form'] = render_to_string('project/function_edit.html', context, request=request)
    return JsonResponse(data)

function_list.html:

{% for function in functions %}
    <tr>
        <td class="text-center">{{ function.code }}</td>
        <td>{{ function.name }}</td>
        {% if request_user_is_business_analyst %}
            <td>
                <a class="btn btn-warning button-handlers" id="js-edit-function-button" data-url="{% url 'project:function_edit' project_code=project.code function_code=function.code %}"><i class="fa fa-pencil" aria-hidden="true"></i> {% trans 'EDIT' %}</a>
                <a class="btn btn-info open-button-handler" href="{{ function.get_absolute_url }}"><i class="fa fa-sign-in" aria-hidden="true"></i> {% trans 'REVERSION' %}</a>
            </td>
        {% endif %}
    </tr>
{% endfor %}

错误:

Traceback (most recent call last):
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
    response = get_response(request)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\revisions.py", line 296, in do_revision_context
    return func(*args, **kwargs)
  File "C:\Users\Nurzhan\PycharmProjects\RMS\project\views.py", line 252, in function_edit
    data['html_function'] = render_to_string('project/function_list.html', {'functions': functions, 'request_user_is_business_analyst': request_user_is_business_analyst})
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\loader.py", line 68, in render_to_string
    return template.render(context, request)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\backends\django.py", line 66, in render
    return self.template.render(context)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py", line 208, in render
    return self._render(context)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py", line 199, in _render
    return self.nodelist.render(context)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py", line 994, in render
    bit = node.render_annotated(context)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py", line 961, in render_annotated
    return self.render(context)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py", line 961, in render_annotated
    return self.render(context)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py", line 994, in render
    bit = node.render_annotated(context)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py", line 961, in render_annotated
    return self.render(context)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\defaulttags.py", line 439, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\urls\base.py", line 91, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\urls\resolvers.py", line 392, in _reverse_with_prefix
    (lookup_view_s, args, kwargs, len(patterns), patterns)
django.urls.exceptions.NoReverseMatch: Reverse for 'function_edit' with arguments '()' and keyword arguments '{'project_code': '', 'function_code': UUID('08abfad3-58a5-4953-8b8f-82bd1f66fecb')}' not found. 1 pattern(s) tried: ['ru/account/dashboard/projects/(?P<project_code>[0-9a-f-]+)/(?P<function_code>[0-9a-f-]+)/edit/$']

我认为你的问题出在这里 首先检查这一行 File "C:\Users\Nurzhan\PycharmProjects\RMS\project\views.py", line 252,

data['html_function'] = render_to_string('project/function_list.html', {'functions': functions, 'request_user_is_business_analyst': request_user_is_business_analyst}) 并打印 request_user_is_business_analyst 以查看 returns

编辑: 解决: 必须是这样的:

data['html_function'] = render_to_string('project/function_list.html', {'functions': functions, 'request_user_is_business_analyst': request_user_is_business_analyst, 'project':project, 'function':function})