2015-09-21 13 views
1

一旦我在我的主視圖中提交我的表單,我們被重定向到我的下載視圖,這對我的會話var有錯誤的響應......爲什麼?我不確定如何解決這個問題,感覺就像我嘗試了一切。django + session var不在視圖之間轉移

@login_required 
def Home(request): 
    form = TimeForm() 
    search_times = [] 
    if request.method == "POST": 

     # clear session data 
     # for key in request.session.keys(): 
     #  test.append(request.session[key]) 

     start = request.POST['start_time'] 
     end = request.POST['end_time'] 
     start = time.mktime(datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S").timetuple()) 
     end = time.mktime(datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S").timetuple()) 


     start = re.split('.[0-9]+$', str(start))[0] 
     end = re.split('.[0-9]+$', str(end))[0] 
     search_times.append(int(start)) 
     search_times.append(int(end)) 

     request.session['search_times'] = search_times 
     request.session.save() 

    context = { 
     'form': form, 
     'search_times': search_times, 
    } 
    return render(request, "pcap_app/home.html", context) 

def Download(request, path): 
    test = [] 
    access_time = [] 
    search_times = [] 

    search_times = request.session.get('search_times', False) 

    # testing timestamp directory listing 
    files = sorted_ls(os.path.join(settings.MEDIA_ROOT, path)) 
    for f in files: 
     time = os.stat(os.path.join(settings.MEDIA_ROOT, f)).st_mtime 
     access_time.append(int(re.split('.[0-9]+$', str(time))[0])) 

    context = { 
     'sort': files, 
     'times': access_time, 
     'search': search_times, 
     'test': test, 
    } 
    return render(request, "pcap_app/downloads.html", context) 

home.html的

<head> 
    <script     src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
    <link href="{{ STATIC_URL }}css/bootstrap.css" rel="stylesheet" type="text/css"/> 
    <script src="{{ STATIC_URL }}js/bootstrap.js"></script> 
    {{ form.media }} 
</head> 
<body> 
    <h1>pcap search</h1> 

    {% if user.is_authenticated %} 

    <form action="/download/" method="POST"> 
    {% csrf_token %} 
    {{ form.as_p }} 
    <input id="submit" type="submit" value="Submit"> 
    </form> 
    {{ search_times }} 
    {{ test }} 
     <a href="/logout/">Logout</a> 

    {% else %} 
     <a href="/login/?next={{ request.path }}">Login</a> 

    {% endif %} 
</body> 
+0

你在哪裏重定向上面的代碼?另外,請修復您的縮進。 –

+0

到我的下載視圖 – ajanakos

+0

我的意思是在上面的視圖中,重定向的代碼在哪裏。此外,在POST請求的情況下會執行哪些代碼段。 –

回答

1

你有錯誤設定了action屬性,在home.html模板/download/

當您提交表單時,將POST請求提交給/download/視圖,而不是home視圖。所以,設置會話的代碼永遠不會執行。

您需要將action屬性更改爲您的home視圖網址。當您這樣做時,如果發生POST請求,將在會話中設置變量search_times

<form action="/home/url/" method="POST"> 

home.html指定action屬性後,您將需要重定向到/download/home視圖。然後在重定向發生在您的download視圖後,您將訪問會話中的可變參數search_times

@login_required 
def Home(request): 
    form = TimeForm() 
    search_times = [] 
    if request.method == "POST": 
     ... 
     request.session['search_times'] = search_times # set the variable in the session 
     return HttpResponseRedirect('/download/') # redirect to download page 

    context = { 
     'form': form, 
     'search_times': search_times, 
    } 
    return render(request, "pcap_app/home.html", context) 
+0

TY :)我刪除了我的表單操作中的字符串,並在我的視圖中添加了一個url重定向 – ajanakos

2

嘗試保存會話

request.session.save() 
+0

已更新我的代碼,添加保存仍然重新調用了一個錯誤 – ajanakos