Answer the question
In order to leave comments, you need to log in
Sending AJAX + Django files?
Hello, I have a problem.
There is a form:
<form id="avatarUpload" action="{% url 'Home:Edit' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="{{ form.avatar.name }}" id="{{ form.avatar.name }}">
</form>
$('.action__button.primary').on('click', function() {
$('#avatarUpload').submit(function() {
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
success: function() {
console.log('OK')
}
})
})
})
if request.method == 'POST':
user_form = UserEditForm(data=request.POST, instance=request.user)
form = ProfileEditForm(data=request.POST, files=request.FILES, instance=request.user.profile)
if form.is_valid and user_form.is_valid:
user_form.save()
form.save()
return redirect('Home:Edit')
if request.is_ajax:
form.save()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question