2012-10-12 62 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