[英]ajax response return with function for
Can you tell me how can I replace code html with function 'for from django'?你能告诉我如何用 function 'for from django' 替换代码 html 吗?
$.ajax({
url: url,
data: $('#FormSite').serialize(),
type: "POST",
async:false,
success: function(response) {
$($("#Pic").first()).replaceWith($(response['Pic']));
$("#HeaderWpis").text(response['h1']);
$("#WpisChild").html("<div id='WpisChild'> {% for News in Messags %} <p>{{ News.title }}</p> </div>");
},
error: function(data)
{
alert('Bad connection');
console.log(data);
}
});
When I do this I got {%for%} from third div 'WpisChild' as text.当我这样做时,我从第三个 div 'WpisChild' 中得到 {%for%} 作为文本。 The function does not perform on the page.
function 不在页面上执行。 Could you tell me why?
你能告诉我为什么吗?
The Django template language is processed when your views are accessed, so JQuery can't interpret it.访问视图时会处理 Django 模板语言,因此 JQuery 无法解释它。 Here are two suggestions:
这里有两个建议:
def get_messages(request):
if request.method == 'POST':
# do whatever with your request, fetch your messages
context = {
'Messages': messages
}
return render(request, 'message-template.html', context)
$("#WpisChild").append(response)
response['Messages'].forEach(function(news) {
$("#WpisChild").append('<p>' + news.title + '</p>')
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.