0

{% url org_select_book_list %}関数 を使用して新しいページに移動する前にreload_books、いくつかの値を投稿する必要があり、データを に保存する必要があります{% url org_store_select_book_list %}

しかし、データを受信できません。

でも にコメントするreload_books();と、受け取ることができます。なんで?

ここでビューを追加しますが、それが機能するかどうかはわかりません。注: はHttpResponseテスト目的のためだけのものです。

function reload_books() {  
        var new_url = '{% url org_select_book_list %}?type=' + type  
                + '&order_by=' + order_by + '&page=' + page  
                + '&per_page=' + per_page + '&q=' + q;  
        for (var i = 0; i < search_param_stack.length; i ++) {  
            new_url += '&' + search_param_stack[i] + '=' + eval(search_param_stack[i]);  
        }  

        window.location.href = new_url;
    }


function change_page(new_page) {
        if (new_page < 1) {
          strong textpage = 1;
        } else if (new_page > {{ book_list.paginator.num_pages }}) {
            page = {{ book_list.paginator.num_pages }};
        } else {
            page = new_page;
        }

        var form = document.createElement("form");
        form.innerHTML = "{% csrf_token %}";
        form.setAttribute("method", "POST");
        form.setAttribute("action", "{% url org_store_select_book_list %}");
        form.setAttribute("enctype", "multipart/form-data");
        //form.setAttribute("Content-Type", "application/x-www-urlencoded");

        var page_from = document.createElement("input");
        page_from.setAttribute("type", "hidden");
        page_from.setAttribute("name", "page_from");
        page_from.setAttribute("value", "{{ book_list.number }}");
        form.appendChild(page_from);

        var bookId = document.getElementsByClassName("batch_cb");
        var page_length = {{ book_list.end_index }} - {{ book_list.start_index }};
        for(var i = 0; i < page_length; i++) {
            var YAhiddenField = document.createElement("input");
            YAhiddenField.setAttribute("type", "hidden");
            YAhiddenField.setAttribute("name", "bookId[" + i + "]");
            if(bookId[i].checked == true) {
                YAhiddenField.setAttribute("value", bookId[i].getAttribute("value"));
            }
            form.appendChild(YAhiddenField);
        }

        document.body.appendChild(form);
        form.submit();
        document.body.removeChild(form);

        reload_books();
        return false;
    }


views.py
@user_type_required(['organizationuser'])
def org_store_select_book_list(request):
    """
    when select a list of books from one page,
    the session will store the books' id from that page
    """

    # initialize the session
    request.session.select_books = []
    # if pass the books' id to this page
    if request.POST:
        # if books' id not in the id list
        for i in request.POST:
            if i.startswith("bookId"):
                if request.POST[i] not in booksId:
                # the list append the book's id
                    booksId.append(request.POST[i])
        # delete the data of the session
        del request.session.select_books
        # store the list to the session
        request.session.select_books = booksId

        from django.http import HttpResponse
        return HttpResponse(booksId)
4

1 に答える 1

0

やったほうがいい

if request.method == 'POST':

それ以外の

if request.POST:
于 2012-08-29T08:30:35.383 に答える