Reputation: 649
it's a just simple task of a flask that picks jpg format file from user & saves to the particular folder but it gives me this error:
jinja2.exceptions.UndefinedError: 'index' is undefined
this is my Flask Code:
import os
from flask import Flask, render_template, request
app = Flask(__name__)
app_root = os.path.dirname(os.path.abspath(__file__))
@app.route("/")
def index():
target = os.path.join(app_root, 'txt/')
print(target)
if not os.path.isdir(target):
os.mkdir(target)
for file in request.files.getlist("mp3_file"):
print(file)
file_name = file.filename
destination = '/'.join([target, file_name])
print(destination)
file.save(destination)
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
& this is my HTML Code(this code could have Bugs but right now flask showing error):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename =
'bootstrap.css') }}" /><!-- ../static/bootstrap.css -->
</head>
<body background="Bg.jpg">
<center>
<form method="post" enctype="multipart/form-data" action="{{ url_for(index) }}">
<table class="table table-hover" style="width:100px; background-color:rgba(102,102,102,0.9); border-radius:10px; margin:100px">
<tr>
<td class="text-right"><label class="text-white" style="margin-top:4%">Upload Your Audio file here: </label></td>
<td><input name="mp3_file" type="file" value="" class="text-white btn" required></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit_btn" value="Submit" class="btn btn-dark" style="margin-left:45%" /></td>
</tr>
<tr>
<td colspan="2">{% if file %}<textarea name="answer" rows="10" cols="100" content="{{ file }}" ></textarea>{% else %} <textarea name="answer" rows="10" cols="100" ></textarea> {% endif %}</td>
</tr>
</table>
</form>
</center>
</body>
</html>
Upvotes: 0
Views: 3015
Reputation: 176
Your error is in action="{{ url_for(index) }}". You are using index as a variable but you are not passing anything to the template. I suppose you want to get the url of index, then wrap index with single quotes and it will work fine i.e: action="{{ url_for('index') }}".
Upvotes: 5