2017-02-12 65 views
0

我在Django中使用重定向將用戶發送到另一個頁面,但無法這樣做。重定向無法發送回覆

views.py

def func1(request): 
    if not request.user.is_authenticated(): 
     return render(request, 'login/login/login.html') 
    else: 
     if request.method == "POST": 
      return redirect('base:func2') 
     return render(request, 'base/home/index.html') 


def func2(request): 
    if not request.user.is_authenticated(): 
     return render(request, 'login/login/login.html') 
    else: 
     if request.method == "POST": 
      .... 
      return render(request, 
          'bla_bla_bla.html', 
          {'location': location}) 
     elif request.method == 'GET': 
      print('COntrol is here') 
      some_details_form = SomeDetailsForm() 
      return render(request, 'some_url/index.html', 
          {'some_details_form': some_details_form}) 

urls.py

app_name = 'base' 
url(r'^another_url/$', views.func2, name='func2'), 
url(r'^some_url/$', views.func1, name='func1'), 

基地的/ home/index.html的

<div class="button" onclick="clicked_it()"> 
    <span id="forward"> Go Further </span> 
</div> 

index.js

function clicked_it() { $.post("/base/some_url/"); };

所以控制不會去FUNC2,因爲我可以看到打印語句輸出控制這裏但我沒有看到FUNC2能夠呈現的頁面中return語句。控制卡在哪裏?

回答

1

使用Ajax的全部意義在於,它覆蓋瀏覽器導航操作。如果你想讓你的Ajax重定向,你需要在js中完成 - 或者根本不要使用Ajax。

+0

你可能會更具體一點做什麼,因爲我是新開發的Web開發人員,並且很少或根本沒有JS知識 –

+0

首先解釋你爲什麼擁有'clicked_it ()'功能,而不是普通的鏈接或按鈕。 –

+0

我正在使用它將數據發送到用戶將在頁面上輸入的後端。 –

0

重定向要麼需要相對或絕對的網址,你需要重定向到。 你需要使用Django反向鏈接解析器

from django.core.urlresolvers import reverse 

def func1(request): 
    if not request.user.is_authenticated(): 
     return render(request, 'login/login/login.html') 
    else: 
     if request.method == "POST": 
      return redirect(reverse('base:func2')) 
     return render(request, 'base/home/index.html') 
+0

沒有..它仍然無法重定向 –