【问题标题】:django jeditable not workingdjango jeditable 不工作
【发布时间】:2012-10-03 00:33:54
【问题描述】:

请帮助我,我正在尝试使用 jeditable 来编辑 {% for in %} 内表中的字段。

可编辑的 DIV:

<td><div class="edit" id="{{ c.id }}">{{ c.name|safe }}</div></td>

可编辑代码:

<script>
    $(document).ready(function(){
      $('.edit').editable('/categoryedit/{{ c.id }}/', {
        style: 'display: inline'    
        });
    });
</script>

网址:

url(r'^categoryedit/(?P<id>\d+)/$', 'pos.views.CategoryEdit'),

查看:

def CategoryEdit(request, category_id):
  id = request.POST.get('category_id', '')
  value = request.POST.get('value', '')

  categoria = Category.objects.get(pk=id)
  categoria.name = value
  categoria.save()

  return HttpResponse(escape(value))

【问题讨论】:

  • 您的具体问题是什么?不仅仅是“不工作”。
  • 该字段变为可编辑,但不保存对数据库的更改,并且当字段失去焦点时也会丢失更改。

标签: django jeditable


【解决方案1】:

您可能需要将 CSRF 数据添加到您的 javascript。我刚刚遇到这个并在这里发布: Django and JEditable: CSRF error

确定查看的一种方法是使用firebug 并查看从 Django 返回的 ajax 响应。 (如果缺少 CSRF 信息,Jeditable AJAX 调用会抛出 403 Forbidden 错误。)

【讨论】:

    【解决方案2】:

    解决方案:问题是可编辑的 DIV 在 {% for %} bucle 内,在这种情况下,需要像这样使用 .each en Javascript...

    $('.edit').each(function(){
                    $('.edit').editable('/categoryedit', {
    
                    });
                });
    

    并且没有必要在url(“/category/1”)中传递参数,而是使用...获取参数更好。

    c_id = request.POST.get('id')
    

    视图必须是这样的:

    def CategoryEdit(request):
    if request.is_ajax():
        if request.method == 'POST':
            txt = request.POST.get('value')
            c_id = request.POST.get('id')
    
            categoria = Category.objects.get(pk=c_id)
    
            categoria.name = txt
    
            categoria.save()
    
            return HttpResponse(txt)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 2015-01-11
      相关资源
      最近更新 更多