Search code examples
djangojeditable

django jeditable not working


PLease i need your help, i'm trying to use jeditable to edit a field on a table inside {% for in %}.

editable DIV:

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

jeditable code:

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

Url:

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

View:

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

Solution

  • Solution : The problem was that the editable DIV was inside a {% for %} bucle and in that case is needed to use .each en the Javascript like this...

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

    and is not necessary to pass the parameters in the url ("/category/1") instead is better to get the parameters using ...

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

    the View must be like this:

    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)