ผมได้ทำการสร้างไฟล์ index.html มีโค้ดดังนี้
โค้ด: เลือกทั้งหมด
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>We have added you number</title>
</head>
<body>
<form id="contact-form" action="/save", method="POST">
<p>color<input type = "text" name = "save" /></p>
<p><input type = "submit" value = "submit" /></p>
</form>
</body>
</html>
โค้ด: เลือกทั้งหมด
from flask import Flask
from flask import render_template
from flask import request
app = Flask(__name__)
@app.route("/")
def index(): #โมดูลรับค่าจากไฟล์ index.html โดยการส่งค่าในรูปแบบ POST
return render_template('index.html')
@app.route('/save', methods=['POST'])
def save(): #โมดูลแสดงข้อมูล
x = dict(request.form.items())
return "รับค่าสำเร็จ %s"%(x)
app.run(debug=True)
โค้ดในส่วนของ Error
โค้ด: เลือกทั้งหมด
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/com012/m049_bank/python_test/M049/result1.py", line 9, in index
return render_template('index.html')
File "/usr/local/lib/python2.7/dist-packages/flask/templating.py", line 133, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 830, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 791, in get_template
return self._load_template(name, self.make_globals(globals))
File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 765, in _load_template
template = self.loader.load(self, name, globals)
File "/usr/lib/python2.7/dist-packages/jinja2/loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "/usr/local/lib/python2.7/dist-packages/flask/templating.py", line 57, in get_source
return self._get_source_fast(environment, template)
File "/usr/local/lib/python2.7/dist-packages/flask/templating.py", line 85, in _get_source_fast
raise TemplateNotFound(template)
TemplateNotFound: index.html
127.0.0.1 - - [02/Aug/2017 17:08:58] "GET /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2017 17:08:58] "GET /?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2017 17:08:58] "GET /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2017 17:08:58] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [02/Aug/2017 17:08:58] "GET /?__debugger__=yes&cmd=resource&f=source.png HTTP/1.1" 200 -