2012-11-16 23 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 –