N
N
nurzhannogerbek2018-02-05 08:32:17
Django
nurzhannogerbek, 2018-02-05 08:32:17

How to fix url error?

Hello, please help me understand.

There is a page in which I deduce the list of documents. A similar page has, for example, the following url address:
GET /documents/1/2009/ HTTP/1.1" 200 126071
where

1- категория документа
2009 - год создания документа


Each document in this list has a button that opens the editing form. After clicking the submit button , the list of documents is updated via ajax. In the terminal, I see the following url requests:
GET /documents/1/2009/edit/1120/ HTTP/1.1" 200 5755

POST /documents/1/2009/edit/1120/ HTTP/1.1" 200 169852

where 1120 is the id of the document selected by the user.

PS The last two url requests can be seen only in the terminal, and the user is shown only the first url address.

The problem starts after updating the list. Many of my scripts are tied to the main page, that is, with the first url address. These scripts do not work after the list has been updated. All subsequent ajax requests are added to the third request. For example:
GET /documents/1/2009/edit/1120/?page=2&querystring_key=page HTTP/1.1" 200 5747


Although it should be like this:
GET /documents/1/2009/?page=2&querystring_key=page HTTP/1.1" 200 5747


Do you see a solution in this case? I do not know in what direction to dig. Attached my code.

main_template.html:
<div id="documents" class="dd">
    <ol class="dd-list">
        {% include page_template %}
    </ol>
</div>

{% block script %}
    {# START: AJAX request to endless pagination #}
    <script type="text/javascript">
        function activatePagination(){
            $.endlessPaginate({
                paginateOnScroll: true,
                onCompleted: function(context, fragment) {
                    activateTooltip();
                }
            });
        }
        activatePagination();
    </script>
    {# END: AJAX request to endless pagination #}

   <script src="{% static 'js/documents/crud.js'%}"></script> {# CRUD JS #}
{% endblock %}


crud.js:
$(function () {
    $("#document-modal").on("submit", ".document-edit-form", function(e) {e.preventDefault(); saveForm.call(this, "Data successfully updated");});

    var saveForm = function(success_message) {
        var form = $(this);
        var formData = new FormData(form[0]);
        $.ajax({
            url: form.attr("action"),
            data: formData,
            type: form.attr("method"),
            dataType: 'json',
            success: function (data) {
                if (data.form_is_valid) {
                    // Update list of documents
                    $("#documents ol.dd-list").html(data.html_documents);

                    activatePagination();

                    // Hide modal
                    $("#document-modal").modal("hide");

                    // Show message to user
                    $("#user-action-message").fadeIn("slow");
                    setTimeout(function() {$("#user-action-message").fadeOut("slow");}, 2000);
                }
            },
            cache: false,
            contentType: false,
            processData: false,
        });
        return false;
    };
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Fedun, 2018-02-15
@igorfedun

show how you generate url
>> url: form.attr("action"),

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question