在上传的文件上执行python代码

时间:2016-06-13 09:57:19

标签: python html django

我有一个django应用程序,我上传了2个文件并希望在它们上运行python脚本(proto2.py)。

在我的html代码中,我添加了一个按钮来执行文件,但它没有工作:

<div>Relay 1:
    <form action="{% url "nettoyage"%}" method="POST">
        {% csrf_token %}
        <input type="submit" value="Toggle" id="toggle1" />
    </form>
</div>

失败原因:     CSRF令牌丢失或不正确。

urls.py:

url(r'^nettoyage/$', 'nettoyage',name='nettoyage'),

views.py:

def nettoyage(request):
    if request.method == 'POST':
        import proto2
    return #Something, normally a HTTPResponse, using django

proto2.py:

file = xlrd.open_workbook('~/Paye_P5_test.xlsx',encoding_override='utf-8')

sheet = file.sheet_by_name('Feuil1')
headers = [str(cell.value) for cell in sheet.row(0)] 

values = []
for rowind in range(sheet.nrows)[1:]:
    values.append([ cell.value for cell in sheet.row(rowind)])

data2=pandas.DataFrame(data=values,columns=headers)
resume=data2['Résumé']
resume = resume.str.lower()
resume = resume.str.replace("'", " ")

remov_punct = str.maketrans({key: None for key in string.punctuation})
resume = resume.str.translate(remov_punct)

resume = html.unescape(resume)

stop_words = get_stop_words('french')
resume = resume.str.split()
resume = resume.apply(lambda x: [item for item in x if item not in stop_words])

# Porter Stemmer Algo
stemmer = SnowballStemmer("french")
resume = resume.apply(lambda x: [stemmer.stem(item) for item in x])
resume[0]

1 个答案:

答案 0 :(得分:0)

问题在于:

<form action="{% url "nettoyage"%}" method="POST">

通过以下方式更改:

<form action="{% url 'nettoyage'%}" method="POST">

双打引号会干扰action属性,因此关闭它并打开报价。通过简单的'引号更改它将解决您的问题。