Ajax с выбиванием формы не работает в django
productscreate.html
<form>
<table border="1">
<tr>
<td>Title:
<input type="text" name="title" id="title" data-bind="value: title"></td>
<br>
</tr>
<tr>
<td>Description:
<textarea name="description" id="description">Description</textarea></td>
<br>
</tr>
<tr>
<td>Image:
<input type="file" name="image" id="image"></td>
<br>
</tr>
<tr>
<td><button type="button" id="submit" data-bind="click: mySubmit">Submit</button></td>
</tr>
</table>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.0/knockout-min.js"></script>
<script>
var formData1 = new FormData();
$(document).on('click', '#submit',function(e){
e.preventDefault()
var viewModel = {
title:ko.observable(),description:ko.observable(),
mySubmit : function(formElement) {
var formData = {
'title' : viewModel.title() ,
'description' : viewModel.description(),
};
formData1.append('image', $('#image')[0].files[0])
$.ajax({
type: "POST",
url: '{% url "productscreate" %}',
data: formData,formData1,
cache: false,
processData: false,
enctype: 'multipart/form-data',
contentType: false,
success: function (){
window.location = '{% url "productslist" %}';
},
error: function(xhr, errmsg, err) {
console.log(xhr.status + ":" + xhr.responseText)
}
});
}
};
ko.applyBindings(viewModel);
</script>
views.py
class ProductsList(ListView):
model = products
context_object_name = 'products'
template_name = "productslist.html"
class ProductsCreate(CreateView):
model = products
fields = ['title','description','image']
template_name = "productscreate.html"
success_url=reverse_lazy('productslist')
class ProductsDetailView(DetailView):
template_name = "productsdetail.html"
queryset = products.objects.all()
context_object_name = 'products'
model = products
Ajax с нокаутом не работает в этом проекте Я хочу отправить форму с помощью ajax с кодом knockout js. Теперь, когда я нажимаю кнопку submit, значение формы не отправляется. Я хочу отправить форму с помощью ajax Я не знаю, где проблема в этом ajax коде. Пожалуйста, помогите мне Заранее спасибо