我使用此类视图处理带有表单的模态窗口。您可以在此处查看此方法的详细说明https://dmorgan.info/posts/django-views-bootstrap-modals/ 一切正常,但提交的数据在重定向到此视图后不会出现在my_orders视图中。 我在Chrome控制台中看到了对/ my_orders /的GET请求。响应包含提交的数据。但似乎这种反应在我看来并没有呈现出来。也许,它是由XHR响应造成的?
同时重定向在此方法中运行正常,重定向后我已经获得了实际的my_orders视图。
def delete_order(request, id):
Order.objects.filter(id = id).delete()
return redirect('currency_exchange.views.my_orders')
views.py
def my_orders(request):
queryset = Order.objects.filter(user=get_user(request))
context = {
"queryset": queryset
}
return render(request, "my_orders.html", context)
class AjaxTemplateMixin(object):
def dispatch(self, request, *args, **kwargs):
if not hasattr(self, 'ajax_template_name'):
split = self.template_name.split('.html')
split[-1] = '_inner'
split.append('.html')
self.ajax_template_name = ''.join(split)
if request.is_ajax():
self.template_name = self.ajax_template_name
return super(AjaxTemplateMixin, self).dispatch(request, *args, **kwargs)
class AddOrderView(SuccessMessageMixin, AjaxTemplateMixin, FormView, CreateView):
model = Order
template_name = 'add_order_form.html'
form_class = OrderForm
success_url = reverse_lazy('my_orders')
success_message = "Way to go!"
def form_valid(self, form):
order = form.save(commit=False)
order.user = self.request.user
order.save()
return redirect('currency_exchange.views.my_orders')
JS:
var formAjaxSubmit = function(form, modal) {
$(form).submit(function (e) {
e.preventDefault();
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
success: function (xhr, ajaxOptions, thrownError) {
if ( $(xhr).find('.has-error').length > 0 ) {
$(modal).find('.modal-body').html(xhr);
formAjaxSubmit(form, modal);
} else {
$(modal).modal('toggle');
}
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
});
}
答案 0 :(得分:0)
你没有展示你的JS。但是,如果您要发送Ajax请求,则需要在JS代码中执行某些操作以实际执行重定向。