2012-11-16 22 views
1

這裏是我的views.py:全局變量不能在Python/Django中工作?

from django.shortcuts import render_to_response 
from django.template import RequestContext 
import subprocess 

globalsource=0 
def upload_file(request): 
    '''This function produces the form which allows user to input session_name, their remote host name, username 
    and password of the server. User can either save, load or cancel the form. Load will execute couple Linux commands 
    that will list the files in their remote host and server.''' 

    if request.method == 'POST':  
     # session_name = request.POST['session'] 
     url = request.POST['hostname'] 
     username = request.POST['username'] 
     password = request.POST['password'] 

     globalsource = str(username) + "@" + str(url) 

     command = subprocess.Popen(['rsync', '--list-only', globalsource], 
          stdout=subprocess.PIPE, 
          env={'RSYNC_PASSWORD': password}).communicate()[0] 

     result1 = subprocess.Popen(['ls', '/home/'], stdout=subprocess.PIPE).communicate()[0] 
     result = ''.join(result1) 

     return render_to_response('thanks.html', {'res':result, 'res1':command}, context_instance=RequestContext(request)) 

    else: 
     pass 
    return render_to_response('form.html', {'form': 'form'}, context_instance=RequestContext(request)) 

    ##export PATH=$RSYNC_PASSWORD:/usr/bin/rsync 

def sync(request): 
    """Sync the files into the server with the progress bar""" 
    finalresult = subprocess.Popen(['rsync', '-zvr', '--progress', globalsource, '/home/zurelsoft/R'], stdout=subprocess.PIPE).communicate()[0] 
    return render_to_response('synced.html', {'sync':finalresult}, context_instance=RequestContext(request)) 

的問題是同步()查看。沒有采用upload_file的全局變量值,但在同步視圖中採用globalvariable = 0。我究竟做錯了什麼?

編輯: 試着做這樣的:

global globalsource = str(username) + "@" + str(url) 

不過,我得到這個錯誤:

SyntaxError at /upload_file/ 
invalid syntax (views.py, line 17) 
Request Method: GET 
Request URL: http://127.0.0.1:8000/upload_file/ 
Django Version: 1.4.1 
Exception Type: SyntaxError 
Exception Value:  
invalid syntax (views.py, line 17) 
+0

爲什麼負號?請解釋!!! – sachitad

+0

查看@ daniel-roseman的回覆。你的方法有安全問題,也可能顯示不正確的數據與多個請求,並可能無法在多進程環境中工作 – Igor

回答

1

如果分配給一個函數的任何位置的變量,巨蟒認爲該變量作爲本地。因此,在upload_file中,您沒有獲得全球globalsource,但創建了一個新的本地函數,在函數結束時被拋棄。

要讓Python即使在分配給它時也使用全局變量,請在您的upload_file函數中添加​​語句。

編輯:這不是你如何使用global聲明。你需要做這在你的函數:

global globalsource 
globalsource = str(username) + "@" + str(url) 
+0

是啊,已經試過了:global globalsource = str(username)+「@」+ str(url)但是,我得到語法錯誤。 – sachitad

+0

@sachitad:然後在你的帖子中包括你嘗試的代碼和你得到的完整的錯誤信息。 – BrenBarn

+0

完成!編輯問題 – sachitad

4

這是一個根本錯誤的做法。你應該不是這樣做。

全局變量將被所有請求訪問。這意味着完全不相關的用戶會看到該變量的先前請求的值。鑑於您正在使用它來訪問用戶的數據,這是一個嚴重的安全風險。

你應該在會話中存儲這樣的元素。

+1

你能給我一個關於如何做到這一點的簡短教程?謝謝 – sachitad

+1

請參閱[文檔](https://docs.djangoproject.com/en/1.4/topics/http/sessions/),但真的很簡單,就像request.session ['source'] = myurl'。 –

+0

@DanielRoseman我有一個關於Django視圖中全局變量的問題。可以請提供您的意見? http://stackoverflow.com/questions/39490843/django-app-level-variables –