Ajax Django-提交表单时重新加载div

Ajax Django-提交表单时重新加载div,ajax,django,Ajax,Django,我试图在提交表单时刷新django应用程序中的某个div index.html <div class="col-md-8" id="notesColumn"> {% crispy note_form %} {% include 'item/item_notes.html' %} </div> $(document).ready(function () { $("#notesTab form").submit(function(event){ ev

我试图在提交表单时刷新django应用程序中的某个div

index.html

<div class="col-md-8" id="notesColumn">
  {% crispy note_form %}
  {% include 'item/item_notes.html' %}
</div>
$(document).ready(function () {
  $("#notesTab form").submit(function(event){
      event.preventDefault();

  $('#notesList').remove();

  $.ajax({
    url: "{% url item_notes %}",
    success: function(data){
      $('#notesColumn').html('data');
    }
  })
})
views.py

def item_notes(request):
  return render_to_response(request, 'candidate/candidate_notes.html')
url.py

url(r'item/profile/(?P<pk>[0-9]+)/$', views.ItemProfile.as_view(), name='item_profile'),
  url(r'item/notes', views.item_notes, name='item_notes'),

您不能在外部JS文件中使用Django模板标记-Django不会解析该文件,这就是为什么您可以看到文字标记被附加到Ajax URL中


您需要在模板本身的内联脚本中将该值设置为全局JS变量。

您不能在外部JS文件中使用Django模板标记-Django不会解析该文件,这就是为什么您可以看到文本标记被附加到Ajax URL中的原因


您需要在模板本身的内联脚本中将该值设置为全局JS变量。

首先,尝试使用绝对URL查看是否是URL导致错误:

  $.ajax({
    url: "item/notes",
    success: function(data){
      $('#notesColumn').html('data');
    }
  })
第二,你为什么会得到notes的URL?你不是在用“物品档案”吗?在这种情况下,请尝试获取该URL:

  $.ajax({
    url: "item/profile/" + "{{ object.pk }}",
    success: function(data){
      $('#notesColumn').html('data');
    }
  })
第三,检查您的JS代码,您在
$(“#notesTab form”)上缺少一个结束括号。提交

第四,尝试转义URL。我不确定该在JS中使用哪种方法,但我多次遇到这个问题,因为它是由于未替换的代码而中断的


这些只是我头顶上的一些提示。希望有帮助。

首先,尝试使用绝对URL查看是否是URL导致错误:

  $.ajax({
    url: "item/notes",
    success: function(data){
      $('#notesColumn').html('data');
    }
  })
第二,你为什么会得到notes的URL?你不是在用“物品档案”吗?在这种情况下,请尝试获取该URL:

  $.ajax({
    url: "item/profile/" + "{{ object.pk }}",
    success: function(data){
      $('#notesColumn').html('data');
    }
  })
第三,检查您的JS代码,您在
$(“#notesTab form”)上缺少一个结束括号。提交

第四,尝试转义URL。我不确定该在JS中使用哪种方法,但我多次遇到这个问题,因为它是由于未替换的代码而中断的


这些只是我头顶上的一些提示。希望对您有所帮助。

您是否尝试过:
{%url'项\u注释“%}
。注意
item\u notes
周围的引号。是的,我试过了。它给了我这个错误:GET 404(未找到)send@jquery.js:9175 ajax@jquery.js:8656(匿名)@candidate_details.js:15 dispatch@jquery.js:4737 elemData.handle@jquery.js:4549也许你在
url(r'item/notes/,…)中缺少一个正斜杠。
你试过这个:
{%url'item\u notes%}
。注意
item\u notes
周围的引号。是的,我试过了。它给了我这个错误:GET 404(未找到)send@jquery.js:9175 ajax@jquery.js:8656(匿名)@candidate_details.js:15 dispatch@jquery.js:4737 elemData.handle@jquery.js:4549也许你在
url(r'item/notes/,…)
谢谢你,我不知道。我需要将哪个值设置为gobal JS变量?“r'item/notes”?您可以只在index.html中使用模板标记url:
var url='{%url'item_notes'%}'url
。谢谢,我不知道。我需要将哪个值设置为gobal JS变量?“r'item/notes”?您可以只在index.html中使用模板标记url:
var url='{%url'item_notes'%}'url
。谢谢Özer!包括{object.pk}不起作用,因为django似乎不包括这些模板标记。谢谢Özer!包括{object.pk}不起作用,因为django似乎不包括这些模板标记。