1

Here's my views.py:

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

The problem is in sync() view. The global variable value from upload_file is not taken but the globalvariable=0 is taken in sync view. What am I doing wrong?

Edit: Tried doing like this:

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

However, I get this error:

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)
2
  • Why negative marking? Please Explain!!! Nov 16, 2012 at 9:05
  • See @daniel-roseman reply. Your approach have security issues, also it may show incorrect data with multiple requests and may not work in multi-process environment
    – Igor
    Nov 16, 2012 at 9:46

2 Answers 2

4

This is a fundamentally mistaken approach. You should not do this.

The global variable will be accessible by all requests. That means a completely unrelated user will see the previous request's value for that variable. Given that you're using this to access a user's data, this is a serious security risk.

You should store elements like this in the session.

3
1

If you assign to a variable anywhere in a function, Python treats that variable as local. So in upload_file you are not getting at the global globalsource, but are creating a new local one that gets thrown away at the end of the function.

To make Python use a global variable even when you are assigning to it, put a global globalsource statement in your upload_file function.

Edit: That's not how you use the global statement. You need to do this in your function:

global globalsource
globalsource = str(username) + "@" + str(url)
3
  • Yeah already tried that:global globalsource = str(username) + "@" + str(url) However I get a syntax error. Nov 16, 2012 at 6:48
  • @sachitad: Then include in your post the code you tried and the complete error message you got.
    – BrenBarn
    Nov 16, 2012 at 6:49
  • @BrenBarn I've a question regarding global variables in Django views. Can please provide your views ? stackoverflow.com/questions/39490843/django-app-level-variables Sep 15, 2016 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.