2012-10-12 59 views
2

PLease我需要你的帮助,我试图用jeditable编辑{%for%}中的表格中的一个字段。django jeditable不工作

编辑DIV:

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

jeditable代码:

<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)) 
+2

你的具体问题是什么?不只是“不工作”。 – deadly

+1

该字段变得可编辑,但不保存对数据库的更改,并且当字段丢失焦点时也丢失更改。 – rpalma

回答

1

解决方案:问题是可编辑的DIV是{%用于%} bucle内,在这种情况下是需要使用EN。每次的JavaScript这样的...

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

       }); 
      }); 

而不必传递参数的URL(“/分类/ 1”),而不是最好使用获得的参数...

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

the Vie w必须是这样的:

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) 
0

你可能需要CSRF数据添加到您的JavaScript。我只是遇到了这个问题,并将其发布在这里: Django and JEditable: CSRF error

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

+0

感谢您的回答! – rpalma