2015-12-08 35 views
0

我想在將數據保存到數據庫時加載圖像。所以,我Ajax打電話來做到這一點。我使用Django 1.8後,把Ajax調用Django成功添加數據後不重定向默認頁面。我檢查數據保存成功和view.py方法也運行。Ajax調用後,Django不重定向給定的網址?

在我向論壇提交無效數據並提交時顯示驗證錯誤之前,將Ajax調用。但現在只查看Ajax錯誤消息。

before add ajax call

但添加Ajax後,現在我進入不提交數據並沒有顯示上述指令(驗證錯誤)。我使用Django默認驗證,他們在創建表單時定義了模型類。

---------------- 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 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(調用方法)---------------- ------------------

@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) 

我測試當我輸入有效數據和提交,每件事情都成功了,但沒有重定向我的網址。它顯示了我在現場的舊數據。

至於,我注意到view.py返回方法不加載。

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

不執行。

請期待一些專家的幫助。

+0

在ajax成功函數中寫入你的重定向url –

+0

Ajax期望來自服務器的響應數據是json,xml或html,而不是重定向函數 –

+0

@saravanann,先生,我是web開發的新角色。所以,你能否提供一些例子,這對我會有很大的幫助。 – uma

回答

1

可能,這將幫助你:

而是這種重定向在views.py的:

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

使用本:

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

最後在阿賈克斯成功函數:

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

(usernam e將是名爲「data」的上下文中的一個關鍵字)。

+0

感謝您的幫助。我把Ajax中的成功方法放在** window.location =「/」**中。現在,重定向很好。 – uma

+1

@uma我的榮幸。 –