Hi, I am trying to AutoSave a field on my form using ajax. I think where I am going wrong is directing ajax to my Django view. Any idea where I am going wrong here?
{% include 'base.html' %}
{% block content%}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
{% for x in cust %}
<tr>
<th>
{{ x.customer.job_name }} <p></p>
</th>
<td>
<form action="" method='post' data-url="{% url 'customer-dashboard' x.id %}">
{%csrf_token%}
<textarea type="content" id="new_note" onchange="autoSave()" name="cust_notes" maxlength="1000" required="" id="id_cust_notes"
class="form-control">{{x.customer.cust_notes}}
</textarea>
</form>
</td>
{%endfor%}
<script type="text/javascript">
function autoSave()
{
var new_note = document.getElementById('new_note').value;
if(new_note!='')
{
$.ajax({
url: 'data-url',
method: 'post',
data: {'cust_notes' :cust_notes},
{
}
});
}
}
</script>
def customerDashboard(request, id):
#TODO: needs to make it fill out the whole form automatically. So take some stips from original creation.
"""View for user to see Customer Page"""
cust = User.objects.filter(id=id)
user_form = User.objects.get(id=id)
if request.method == 'POST':
form = CustomerForm(request.POST, instance=user_form)
if (form.is_valid()):
new_post = form.save(commit=False)
form.instance.cust_notes = new_post
form.save(update_fields=['cust_notes'])
messages.success(request, "New Note Saved" )
form = CustomerForm()
return render(request,'employees/customer_dashboard.html', {"cust": cust,"form": form})
path('customer_dashboard/<id>', views.customerDashboard, name='customer-dashboard'),
Thank you for any guidance on this. I literally have been trying to do this for a year. If there is another method, I am open to that.