ขอสอบถาม error ของ flask ครับ

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderators: mindphp, ผู้ดูแลกระดาน

benzas00123
PHP Hero Member
PHP Hero Member
Posts: 101
Joined: 06/01/2020 9:58 am

ขอสอบถาม error ของ flask ครับ

Post by benzas00123 » 15/01/2020 10:44 am

ผมลองศึกษาแล้วเขียน code ตามจาก กระทู้นี้
https://www.mindphp.com/%E0%B8%9A%E0%B8 ... -form.html

แต่ไม่สามารถ แสดงผลออกมาได้มันขึ้น

/home/com003/Desktop/Ben/venv/bin/python /home/com003/Desktop/Ben/result.py
* Serving Flask app "result" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 313-926-543
127.0.0.1 - - [15/Jan/2020 10:40:41] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/com003/Desktop/Ben/result.py", line 11, in index
return render_template('index.html')
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/templating.py", line 138, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/jinja2/environment.py", line 869, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/jinja2/environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/jinja2/environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/jinja2/loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/templating.py", line 60, in get_source
return self._get_source_fast(environment, template)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/templating.py", line 89, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: index.html
127.0.0.1 - - [15/Jan/2020 10:40:41] "GET /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [15/Jan/2020 10:40:41] "GET /?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [15/Jan/2020 10:40:41] "GET /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [15/Jan/2020 10:40:42] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -

ครับแล้วในหน้า เว็บ มันเป็นแบบนี้
jinja2.exceptions.TemplateNotFound
jinja2.exceptions.TemplateNotFound: index.html

Traceback (most recent call last)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/com003/Desktop/Ben/result.py", line 11, in index
return render_template('index.html')
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/templating.py", line 138, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/jinja2/environment.py", line 869, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/jinja2/environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/jinja2/environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/jinja2/loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/templating.py", line 60, in get_source
return self._get_source_fast(environment, template)
File "/home/com003/Desktop/Ben/venv/lib/python3.6/site-packages/flask/templating.py", line 89, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: index.html

ครับ
code

Code: Select all

#-*- coding=utf-8 -*-
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)
result.py

Code: Select all

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>We have added your numbers</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>
index.html

User avatar
chatee supasand
PHP VIP Members
PHP VIP Members
Posts: 1348
Joined: 04/06/2019 10:06 am

Re: ขอสอบถาม error ของ flask ครับ

Post by chatee supasand » 15/01/2020 10:48 am

เอาไฟล์ index.html ไว้ถูกโฟล์เดอรรึเปล่าครับ
Image

benzas00123
PHP Hero Member
PHP Hero Member
Posts: 101
Joined: 06/01/2020 9:58 am

Re: ขอสอบถาม error ของ flask ครับ

Post by benzas00123 » 15/01/2020 10:50 am

chatee supasand wrote:
15/01/2020 10:48 am
เอาไฟล์ index.html ไว้ถูกโฟล์เดอรรึเปล่าครับ
ผมเอาไว้ใน folder template ครับ
C++ & java & Python-1.png
C++ & java & Python-1.png (4.84 KiB) Viewed 25 times

User avatar
chatee supasand
PHP VIP Members
PHP VIP Members
Posts: 1348
Joined: 04/06/2019 10:06 am

Re: ขอสอบถาม error ของ flask ครับ

Post by chatee supasand » 15/01/2020 10:52 am

templates มี s นะ
Image

benzas00123
PHP Hero Member
PHP Hero Member
Posts: 101
Joined: 06/01/2020 9:58 am

Re: ขอสอบถาม error ของ flask ครับ

Post by benzas00123 » 15/01/2020 10:53 am

ได้แล้วครับ พอดีพิมพ์ ชื่อ folder ผิด มันต้องเป็น templates ขอบคุณครับ

Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Programming - C/C++ & java & Python”

Who is online

Users browsing this forum: Bing [Bot] and 20 guests