[英]Using ajax post/get call to send current form data to a function
I am trying to keep track of the number of results per page as I navigate through pages by sending them to a function in Django. 当我浏览页面时,我试图通过将它们发送到Django中的函数来跟踪每页结果的数量。 From my understanding the best way to do this is to use an AJAX GET or POST call, but I can't seem to get it working.
据我了解,执行此操作的最佳方法是使用AJAX GET或POST调用,但我似乎无法使其正常运行。
I have a function called myFunction
in my views.py
file that I want to pass my number of results per page to so I can manage the pagination between pages. 我的
views.py
文件中有一个名为myFunction
的函数,我希望将每页的结果数传递给该函数,以便我可以管理页面之间的分页。 Here is my code: 这是我的代码:
In my javascript file: 在我的JavaScript文件中:
function navigate_pages() {
$.ajax({
url: 'autotester/myFunction',
type: 'GET',
data: num_results,
success: function(data){} // I think this is legal to say
});
}
In my HTML: 在我的HTML中:
{% if page.has_previous %}
<a href="?page={{ page.previous_page_number }} onclick="navigate_pages()">prev</a>
{% endif %}
{{ page.number }} of {{ page.paginator.num_pages }}
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}" onclick="navigate_pages()">next</a>
{% endif %}
In views.py
: 在
views.py
:
def myFunction(request):
if request.method == 'POST':
num_results = int(request.POST.get('num_results'))
paginator = Paginator(tuple_table, num_results)
print "\n\POST: " + str(request.POST) + "\n\n"
else:
num_results = int(request.GET.get('num_results'))
paginator = Paginator(tuple_table, 10)
print "\n\GET: " + str(request.GET) + "\n\n"
where tuple_table
is a tuple representation of a dictionary that has my objects that I am paginating. 其中
tuple_table
是字典的元组表示形式,其中包含我要分页的对象。 What makes me think it's not working is when I print the request.GET
I don't see the num_results
parameter that I thought I was passing in with my ajax call. 是什么让我认为它不起作用是当我打印
request.GET
得到的num_results
我没有看到我以为我通过ajax调用传入的num_results
参数。 Plus, I get an error for trying to use a NoneType
(because there is no num_results
parameter). 另外,由于没有使用
num_results
参数,我在尝试使用NoneType
错误。 I feel like Django and jQuery/Ajax should make this process relatively simple, but my total lack of experience with JavaScript is making this extremely difficult for me. 我觉得Django和jQuery / Ajax应该使此过程相对简单,但是我对JavaScript的完全缺乏经验使这对我来说极为困难。
If anyone could either point out where I am going wrong or has a link to a more elegant or correct solution that would be greatly appreciated. 如果有人可以指出我要去哪里,或者可以链接到更优雅或更正确的解决方案,将不胜感激。
The error lies indeed within the javascript, you should pass a collection of key-value pairs instead of just a value. 错误确实在javascript中,您应该传递键值对的集合,而不仅仅是一个值。
Furthermore you do not want to put this on a hyperlink element, because the click on a hyperlink element a
will trigger a page reload on the new url which in your case is "?page={{ page.next_page_number }}"
此外,您不希望将其放在超链接元素上,因为单击超链接元素
a
会触发重新加载新网址的页面,在您的情况下,该网址为"?page={{ page.next_page_number }}"
You might want to put it on a button element and pass the value in the ajax call: 您可能希望将其放在按钮元素上,并在ajax调用中传递值:
{% if page.has_previous %}
<button onclick="navigate_pages( {{ page.previous_page_number }})">prev</button>
{% endif %}
{{ page.number }} of {{ page.paginator.num_pages }}
{% if page.has_next %}
<button onclick="navigate_pages({{ page.next_page_number }})">next</button>
{% endif %}
The correct version should be: 正确的版本应为:
function navigate_pages(page_number) {
num_results=5;
$.ajax({
url: '/myFunction',
type: 'GET',
data: {
"num_results":num_results,
"page_num":page_number
},
success: function(data){} // I think this is legal to say
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.