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 回答