AJAX不更新页面

I have strange problem in my project. Can someone say why it happens and how to fix it.

I have function in view for edit/update object (function_edit). Also I use AJAX to update list of objects (function_list) after success edit and also I use django-reversion app to controll the changes of each object. The function_list has part which is visable only to users with a special role (request_user_is_business_analyst). When I use request_user_is_business_analyst in view it raise error and when I dont use it view function works fine. Also when I didnt use django-reversion request_user_is_business_analyst worked fine.

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 %}

ERROR:

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-packageseversionevisions.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\urlsesolvers.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/$']

1个回答


我认为您的问题就在这里
首先检查这一行
文件“ C:\ Users \ Nurzhan \ PycharmProjects \ RMS \ project \ views.py”,第252行,</ code> </ p>

data ['html_function'] = render_to_string('project / function_list.html',{'functions':functions,'request_user_is_business_analyst':request_user_is_business_analyst})
</ code>
并打印 request_user_is_business_analyst </ code>以查看返回的内容</ p>

编辑:
解决:
必须是这样的:</ p>

  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})
</ code> </ pre>
     </ div>

展开原文

原文

I think that your problem is here First check this line 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}) and print request_user_is_business_analyst to see what returns

EDIT: Solve: must be like this:

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})

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐