xsrf form html,python – tornado开启了xsrf_cookies,在ckeditor中上传文件如何传入xsrf_form_html()?…
大家好,又见面了,我是你们的朋友全栈君。
tornado在setting中设置了”xsrf_cookies” : True,则需要在表单中添加{% module xsrf_form_html() %}。
但ckeditor如何传xsrf_cookies这个值,每次上传图片都显示’_xsrf’ argument missing from POST。
如果把”xsrf_cookies”设置为False则上传成功。
下面是上传的代码
class ckuploadHandeler(BaseHandler):
@authenticated
def gen_rnd_filename(self):
filename_prefix = datetime.datetime.now().strftime(‘%Y%m%d%H%M%S’)
return ‘%s%s’ % (filename_prefix, str(random.randrange(1000, 10000)))
@authenticated
def post(self):
“””CKEditor file upload”””
error = ”
url = ”
callback = self.get_argument(“CKEditorFuncNum”)
if self.request.method == ‘POST’ and ‘upload’ in self.request.files:
fileobj = self.request.files[‘upload’]
fname, fext = os.path.splitext(fileobj[0][‘filename’])
rnd_name = ‘%s%s’ % (self.gen_rnd_filename(), fext)
filepath = os.path.join(self.settings[‘static_path’], ‘upload’, rnd_name)
# 检查路径是否存在,不存在则创建
dirname = os.path.dirname(filepath)
if not os.path.exists(dirname):
try:
os.makedirs(dirname)
except:
error = ‘ERROR_CREATE_DIR’
elif not os.access(dirname, os.W_OK):
error = ‘ERROR_DIR_NOT_WRITEABLE’
if not error:
print(filepath)
with open(filepath,’wb’) as up: #有些文件需要已二进制的形式存储,实际中可以更改
up.write(fileobj[0][‘body’])
urlpath = filename=’%s/%s’ % (‘upload’, rnd_name)
url = self.static_url(urlpath)
print(url)
else:
error = ‘post error’
res = “””
window.parent.CKEDITOR.tools.callFunction(%s, ‘%s’, ‘%s’);
“”” % (callback, url, error)
self.write(res)
Handler的URL (r’/upload/’, ckuploadHandeler),
显示错误`WARNING:tornado.general:403 POST /upload/?CKEditor=context&CKEditorFuncNum=1&langCode=zh (127.0.0.1): ‘_xsrf’ argument missing from POST
`
如何在上传图片的时候把xsrf_cookies也post过去?
html代码:
CKEDITOR.replace(‘context’);
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/144575.html原文链接:https://javaforall.cn
相关文章
- Python-基础06-文件操作
- 2022年最新Python大数据之Python基础【八】文件的操作与类
- python上传文件方法总结
- python进入文件目录 命令_python创建目录
- python写入txt文件中文乱码_python中怎么输入文件
- python怎么读取xlsx文件_arcgis地理加权回归
- python秒级创建httpserver和fp文件服务器
- Python 打开文件对话框「建议收藏」
- python 快速读取压缩包内文件
- crontab 用法(执行python文件)[通俗易懂]
- python读写、创建文件、文件夹等等
- python秒起https 文件服务器
- Python之文件操作大全
- python 去掉文件后缀名,python 删除后缀名文件
- python处理xps文件_如何在Windows 10系统中处理XPS文件
- python shutil删除_python删除文件
- python读取文件如何去除空格_python读取txt文件时怎么去掉空格
- 用python画圣诞树、樱花树、卡通图案及打包成exe文件[通俗易懂]
- python读取txt文件,将文件中第一列显示出来「建议收藏」
- 一文搞定Python读取文件的全部知识