在Ajax调用之后,django是否无法重定向给定的URL?

编程入门 行业动态 更新时间:2024-10-27 18:32:43
本文介绍了在Ajax调用之后,django是否无法重定向给定的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在将数据保存到数据库中的同时放入加载图像.所以,我给Ajax打电话去做.我使用Django 1.8,并在成功添加数据后将Ajax调用Django而不重定向到默认页面.我成功检查了save中的数据,并运行了view.py方法.

I want to put loading image while saving data in database. So ,I Ajax call to do it. I use Django 1.8 and after put Ajax call Django not redirect default page after adding data successfully.I check data in save successfully and view.py method also run.

在我向论坛提交无效数据以及提交无效数据之前,先进行Ajax调用,然后显示验证错误.但现在仅查看Ajax错误消息.

Before put Ajax call when I submit invalid data to forum and when submit it ,show validation errors.but now only view Ajax error message.

但是添加ajax之后,现在我输入没有数据的提交没有显示在上面的指令中(验证错误).我使用Django默认验证,创建表单时它们是定义的模型类.

but after add ajax , now I enter submit without data didn't show above instruction(validation error). I used Django default validation an they are defined model class when create form.

---------------- keylist.html ---------------------------- -

----------------keylist.html------------------------------

{ % extends "base.html" %} {% load crispy_forms_tags %} {% block title %} {% if create %}Create{% else %}New Key{% endif %}Serious {% endblock %} {% block heading %} <h2> Create New Serial Keys </h2> {% endblock %} {% block content %} {% if create %} {% url "marcador_key_create" as action_url %} {% else %} {% url "marcador_bookmark_search" pk=form.instance.pk as action_url %} {% endif %} <form action="{{ action_url }}" method="post" accept-charset="utf-8" id="createForm" > {{ form|crispy }} {% csrf_token %} <div id="submit_loader"></div> <p> <b>Expiry Date*:</b> <input type="date" id="datepicker" name="expier_date"></p> <p><input type="submit" class="btn btn-default" value="Save" id="save_button"> </p> </form> {% endblock %}

---------- base.html(ajax方法)--------------------------------- ----

-------base.html (ajax method )-------------------------------------

<script> // Attach a submit handler to the form $("#createForm").submit(function (event) { event.preventDefault(); //var formData = new FormData($("#createForm")[0]); var serverUrl =$("#createForm").attr('action'); $.ajax({ url: serverUrl, type: 'POST', data: $("#createForm").serialize(), beforeSend: function() { $('#submit_loader').css('display','block'); }, complete: function(){ $('#submit_loader').css('display','none'); }, success: function (returndata) { alert("succsesfully generate keys"); //window.location("xsd"); //return false; }, error: function(data){ alert("please ,check you fill form correctly"); } }); //return false; }); </script>

--------- view.py(调用方法)------------------------------- ---

---------view.py (calling method )----------------------------------

@login_required def key_create(request): #print(request.POST) if request.method == 'POST': form = KeyGenarateForm(data=request.POST) expier_date = request.POST['expier_date'] #print(expier_date=="") if(expier_date==""): form.is_valid= False; if form.is_valid(): #request.POST._mutable = True Key_Gen = form.save(commit=False) Key_Gen.save(expier_date) return redirect('marcador_bookmark_user',username=request.user.username) else: print('form not valied') return render('marcador_bookmark_user',username=request.user.username) else: form = KeyGenarateForm() context = {'form': form, 'create_key': True} return render(request, 'marcador/key_genarate_form.html', context)

我测试了何时输入有效数据并成功提交,但没有重定向我的网址.它在该字段中显示了我的旧数据.

I test when I enter valid data and submit , every thing successfully , but didn’t redirect my url.It show my old data in the field.

作为,我注意到view.py返回方法未加载.

As,I noticed view.py return method not loading.

return redirect('marcador_bookmark_user',username=request.user.username)

不执行.

请希望得到专家的帮助.

please , expect some expert help.

推荐答案

可能会帮助您:

而不是views.py中的重定向:

Instead of this redirection in views.py:

return redirect('marcador_bookmark_user',username=request.user.username)

使用此:

return HttpResponse(json.dumps([{username=request.user.username}]),mimetype='text/json')

最后介绍ajax成功功能:

At last on ajax success function:

window.location.href = '/url-path'+data.username;

(用户名是上下文中名为"data"的键).

(username will be a key inside the context named "data").

更多推荐

在Ajax调用之后,django是否无法重定向给定的URL?

本文发布于:2023-10-10 12:02:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   Ajax   django   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!