gpt4 book ai didi

jquery - 来自 Django 模板的 Ajax JQuery 请求和响应

转载 作者:行者123 更新时间:2023-12-01 01:38:02 25 4
gpt4 key购买 nike

根据jro的建议,我正在修改我的问题和问题。

==网址==

url('^$','pMass.views.index', name='index')  #Index is the main view with form input type text and submit
#If search value is match then it will render result.html template

==查看==

#View will find search value and render result.html template if request is not ajax
#If request is ajax then same view will create new queryset and render to same template page (result.html)

def index(request):
error = False
cid = request.GET

if 'cnum' in request.GET:
cid = request.GET['cnum']

if not cid:
error = False
expcount = Experiment.objects.count()
allmass = SelectedIon.objects.count()

if request.is_ajax():
value = request.GET.get('value')
if value is None:
result = SelectedIon.objects.filter(monoiso__iexact=value).select_related()
template = 'result.html'
data = {
'results':result,
}
return render_to_response(template, data,
context_instance=RequestContext(request))

else:

defmass = 0.000001
massvalue = float(cid)
masscon = defmass * massvalue
highrange = massvalue + masscon
lowrange = massvalue - masscon

myquery = SelectedIon.objects.select_related().filter(monoiso__range=(lowrange, highrange))
querycount = myquery.count()

return render_to_response('result.html', {'query': cid, 'high':highrange, 'low':lowrange, 'sections':myquery, 'qcount':querycount, })

return render_to_response('index.html', {'error': error, 'exp': expcount,'mass':allmass,})

==结果.html==

# I have divided template into two container: main (left) & container (right)
# Left container is for search match (e.g value/title) with hyperlink
# Right container is for detail of the match value
# For right container I have created jQuery tabs (tab1, tab2, tab3)
# The content of the right container in the tab will update according to the link in the left.
#Layout is given below

! tab1 ! tab2 ! tab3 !
-------------------------------------------------------------------------
! 434.4456 ! Show Default Match 1 Record !
! 434.4245 ! & left hyperlink onclick show it's value record !
! 434.4270 ! detail. I have design tab with JQuery !
! 434.2470 ! !
! 434.4234 ! !
-------------------------------------------------------------------------

==左容器(result.html)==

#I am looping through the queryset/list of values that was match with template for tag
#The template variable is given a hyperlink so after clicking it's detail information
will be shown on right container

<script type="text/javascript" src="/media/jquery-1.2.6.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#a.id').click(function(){
value = $ ('a.id').val();
$('div#tab_content')empty().load('{%url index%}?query'= +value);
});
});

<div id="main">
<table align="center" cellspacing="0" cellpadding="4" border="1" bordercolor="#996600">
<tr>
<th>Match</th>
</tr>
{% for section in sections %}
<tr>
<td><a href="#" id="{{%section.monoiso%}}">{{section.monoiso}}</a></td>
</tr>
{% endfor %}
</table>
</div>

==问题==

  • 我不知道如何获取超链接值!
  • 超链接(左容器)上的 jQuery ajax 请求不起作用
  • 我不确定从 result.html 加载索引 View 是否正确
  • 我使用 <a href.... id="{{%section.monoiso%}}" 从超链接向服务器发送数据,如何使用相同的 id 值索引 View 中进行查询并在 result.html 中进行响应?
  • 如何在正确的容器中渲染响应?

欢迎提出建议、评论和答复。

最佳答案

一些出发点。首先,通过 Ajax 调用的 View 不一定要返回 json 对象:数据也可以使用 django.http.HttpResponse 作为字符串返回。 (或 render_to_response ,归结为同一件事)。这意味着您还可以照常返回完整生成的模板。

假设您发布的 View 位于 /index/(?<tab>\d+)/(?<match>\d+)/ (tab 是选项卡索引,match 是匹配索引),您的 JavaScript 可能如下所示:

$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active"); // Remove any "active" class
$(this).addClass("active"); // Add "active" class to this tab
$(".tab_content").hide(); // Hide all tab content

// Construct the url based on the current index
match_name = $("ul.match li").index($("ul.match li.active"));
url = "/index/" + $("ul.tabs li").index($(this)) + "/" + match_name + "/";

// Asynchronous ajax call to 'url'
new $.ajax({
url: url,
async: true,
// The function below will be reached when the request has completed
success: function(transport)
{
$(".tab_content").html(transport); // Put data in the div
$(".tab_content").fadeIn(); // Fade in the active content
}
});
});

我没有对此进行测试,但按照这些思路应该可以。请注意,为了使其适用于您当前的代码,您可以查看 index函数需要允许一个参数。如果您只想使用每个选项卡的同一页面进行测试,请将 url 设为如下所示:url = "/index/";这样它很可能会立即起作用。

关于jquery - 来自 Django 模板的 Ajax JQuery 请求和响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8310984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com