I am new to JS, HTML, Django and all that related stuff, and I was not able to solve my problem on my own by reading documentation or using Google.
I want to save inline-changes using x-editable to the database in an django environment. The template below works perfectly, but now I would like to overwrite the database entries with the new names.
I tried to reduce the code to the relevant parts of my problem.
models.py:
class MyClass(models.Model):
name = models.CharField(max_length=120)
views.py:
def list(request):
return render(request, 'list.html', {'list':MyClass.objects.all()}
urls.py:
url(r'^list/$', 'myapp.views.list', name='list'),
list.html:
{% extends "base.html" %}
{% block content %}
<script>
$(document).ready(function() {
$.fn.editable.defaults.mode = 'inline';
$('.name').editable({
});
});
</script>
<h1>Names</h1>
<div>
<table border='1'>
{% for l in list %}
<tr><td><a class="name">{{ l.name }}</a></td></tr>
{% endfor %}
</table>
</div>
{% endblock %}
--------------------------------------------------------
My most promising approach was to create an update view.
def list_update(request, pk):
l = get_object_or_404(MyClass, pk=pk)
form = ListForm(request.POST or None, instance=l)
if form.is_valid():
form.save()
return redirect('list')
return render(request, '', {'form':form})
and add the following lines to the code above:
urls.py
url(r'^update/(?P<pk>\d+)$', 'myapp.views.list_update', name='list_update'),
list.html
$('.name').editable({
pk: l.pk,
url: '{% url 'list_update' l.pk%}',
});
But this attempt results in an NoReverseMatch error and l.pk seems to be empty. I appreciate any help on how to do this in the right way.