Rjz*_*eng 0 python django views django-views python-2.7
我试图将我在Django的views.py文件中的值传入我写的另一个python脚本,但我不知道该怎么做.这是来自views.py文件的代码:
def get_alarm_settings(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
alarm = Alarm.objects.all()[0].alarm_setting
response = HttpResponse(alarm, content_type='text/plain')
response_a = execfile('alarm_file.py')
return response_a
Run Code Online (Sandbox Code Playgroud)
我试图将响应传递给alarm_file.py,有没有人知道如何做到这一点?
根据问题的评论,在alarm_file.py中创建函数并导入它.要进行python alarm_file.py调用,请使用if __name__ == '__main__':alarm_file.py.它将包含调用时运行的逻辑python alarm_file.py.此外,您可以使用sys.argv获取参数,在命令行中传递.例如:
alarm_file.py:
import sys
def do_something(val):
# do something
print val
# return something
return val
if __name__ == '__main__':
try:
arg = sys.argv[1]
except IndexError:
arg = None
return_val = do_something(arg)
Run Code Online (Sandbox Code Playgroud)
这将确保您可以简单地运行: python alarm_file.py some_argument
在您的视图中只需从alarm_file导入函数并使用它:
from alarm_file import do_something
...
response = HttpResponse(alarm, content_type='text/plain')
response_a = do_something(response)
return response_a
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9733 次 |
| 最近记录: |