N
N
nurzhannogerbek2017-03-30 08:39:44
JavaScript
nurzhannogerbek, 2017-03-30 08:39:44

Why doesn't AJAX refresh the page?

Hello! Help to understand the following strange behavior.

There is a function_edit function to edit a record that is used in the model window. After successful editing, the list of records is updated via AJAX. It also uses the django-reversion application to commit changes. In the page, in the list of records, there is a part that is visible only to a user with a certain role. In my case, if the user is a business analyst, then two buttons are available to him.

The function has request_user_is_business_analystA that is responsible for defining the user's role. But this entry throws an error. Without it, everything works, but does not show that part of the page to a specific user. The problem appeared when I included the django-reversion application. Without it, request_user_is_business_analyst did not raise an error and the list was updated. How can this strange behavior be explained and how to fix it?

URLs.py:

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


view.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:
{% 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-packages\reversion\revisions.py", line 296, in do_revision_context
return func(*args, **kwargs)
File "C:\Users\Nurzhan\PycharmProjects\RMS\project\views.py", line 253, 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 '{'function_code': UUID('08abfad3-58a5-4953 -8b8f-82bd1f66fecb'), 'project_code': ''}' not found. 1 pattern(s) tried: ['ru/account/dashboard/projects/(?P[0-9a-f-]+)/(?P[0-9a-f-]+)/edit/$']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nurzhannogerbek, 2017-03-30
@nurzhannogerbek

Found my mistake. The question is closed.
It was necessary to add:

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question