Hi all, I included in my Django based Web-App a note function, which works fine in Visual Studio. Unfortunately, it doesnt work on the server. I am using ajax, but tested it without ajax but it failed as well. Here is my code:
Template:
<form method = "POST" id="notes-form" class="hide-low-resolution">
{% csrf_token %}
<small class ="text-muted" style="margin-left: 20px;">
Notes
<button class="btn-mini" style="Background-color: transparent;" onclick="change_div_visibility('notes-id')">
<svg >(removedfor a better overview) </svg>
</button>
</small>
{% if view.user_note_func == None %}
<textarea name ="textarea-note" id= "notes-id" class="form-control note-section z-depth-1" rows="12" placeholder="Write something....." ></textarea>
{% else %}
<textarea name ="textarea-note" id= "notes-id" class="form-control note-section z-depth-1" rows="12" placeholder="Write Something..." >{{ view.user_note_func }}</textarea>
{% endif %}
</form>
JS:
$.ajaxSetup({
data: {csrfmiddlewaretoken: '{{ csrf_token }}' },
});
$(function() {
$("#notes-form").submit(function(event) {
// Stop form from submitting normally
event.preventDefault();
var notesForm = $(this);
// Send the data using post
var posting = $.post( notesForm.attr('action'), notesForm.serialize() );
// if success:
posting.done(function(data) {
// alert('success');
});
// if failure:
posting.fail(function(data) {
// failure
// alert('Failuer')
});
});
});
$( "#notes-id" ).focus(function() {
notes_text_old = $('#notes-id').val();
});
$("#notes-id").blur(function() {
notes_text = $('#notes-id').val();
if(notes_text != notes_text_old) {
$("#notes-form").submit();
}
});
View: (Listview-Post: )
def post(self, request):
if request.user.is_authenticated and 'textarea-note' in request.POST:
txt_area_str = self.request.POST.get('textarea-note')
Profile.objects.filter(id=self.request.user.id).update(user_note=txt_area_str)
print(txt_area_str)
if request.is_ajax():
print("is ajax")
return redirect('home')
else:
return redirect('home')
Server-Log: 2021-05-07 13:45:04 Test 2021-05-07 13:45:04 is ajax
I doesnt receive any new entry in the Error-Log.
Does anybody has an idea why this doesn't work on the server? I couldn't find any solution.
Best regards