celianou celianou - 1 year ago 74
HTML Question

Execute python code on uploaded files

I have a django application where I upload 2 files and want to run a python script on them (proto2.py).

In my html code I added a button to execute a file but it didn't work :

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

Reason given for failure:
CSRF token missing or incorrect.

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)])

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])

Answer Source

Problem lies here :

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

Change it by :

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

The doubles quotes interfere with the action attribute, therefore closing it and leaving a quote open. Changing it by a simple ' quote will resolve your issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download