weixin_33670713 2017-03-30 07:14 采纳率: 0%
浏览 55

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条回答 默认 最新

  • weixin_33725126 2017-03-30 08:56
    关注

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

报告相同问题?

悬赏问题

  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿