2016-06-13 36 views
1

我有一個django應用程序,我上傳2個文件並且想在它們上運行一個python腳本(proto2.py)。在上傳的文件上執行python代碼

在我的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] 
+0

請顯示您的'urls.py'文件。 –

+0

「{%url」nettoyage「%}'生成的網址是什麼? –

+0

這一個:url(r'^ nettoyage/$','nettoyage',name ='nettoyage'), – celianou

回答

0

問題就在這裏:

​​

通過改變它:

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

的雙引號,干擾了action屬性,因此關閉它,並留下一個開放的報價。通過簡單的'報價更改它可以解決您的問題。

+0

完美。它完美的作品。我們很快糾正它:) 只是:謝謝! – celianou

+0

不客氣:) –