4

I've inherited a python command line script "myscript.py" which has a number of classes and contains the following:

def main():
    parser = OptionParser()
    parser.add_option("-a", "--all", dest="saveall",
                      help="Store all information", action="store_true",
                      default=False)
    options, args = parser.parse_args()
    output = options.output
    count = options.count
    saveall=options.saveall

    grabber = Grabber()
    grabber.run(count, output, saveall)
if __name__ == '__main__':
    main()

I've tested this on the command line and it works. I want to turn it into a django app. I can put all the classes onto the app's view easily, but I'm not sure how to refactor the main function to trigger the app with the various options that are available via an http request to django. assuming I pass in the arguments with something like:

@csrf_exempt
def myscript(request):

    option1 = str(request.POST.get( option1, False))
    option2 = str(request.POST.get(option2, False))

How can I run myscript from within django, passing the options to it with minimal changes to the script?

3
  • 1
    I'd read the Django tutorial, which explains how to use django to process URL provided arguments, json variables in the body, or how to process query strings (ways to pass data from the client to your view) and then run the view accordingly.
    – jvc26
    Jun 10, 2015 at 20:40
  • 1
    Sorry I wasn't clear. Please see my edited question. Jun 10, 2015 at 21:18
  • Have you tried to write a custom Django command?
    – pythad
    Jun 10, 2015 at 21:52

0

Your Answer

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

Browse other questions tagged or ask your own question.