简体   繁体   中英

500 Internal Server Error When Using Flask and Jinja

I'm currently self-teaching myself Python using a book called "Head First: Python (2nd Edition)" and so far it has been good, but I'm currently stuck in the early stages of building a simple webapp. The webapp allows one to enter in a phrase and letters and then outputs the intersection of both. Since a good portion of the book builds off of this, I can't move forward by skipping this. I've been trying to find an error, but to no avail.

All the code is provided by the book at: http://python.itcarlow.ie/ed2/ch05/webapp/

The file vsearch4web.py in the folder is the final version, so don't use that. This is where I am at instead with my vsearch4web.py folder:

from flask import Flask, render_template
from vsearch import search4letters

app = Flask(__name__)

@app.route('/')
def hello() -> str:
    return 'Hello world from Flask!'

@app.route('/search4')
def do_search() -> str:
    return str(search4letters('life, the universe, and everything','eiru,!'))

@app.route('/entry')
def entry_page() -> 'html':
        return render_template('entry.html',the_title='Welcome to search4letters on the web!')

app.run()

I have set up the folder structure as instructed:

webapp folder --> vsearch4web.py static folder (subfolder of webapp) --> hf.css (from "static") templates folder (subfolder of webapp) --> base.html, entry.html and results.html (from "template")

The files in the static folder and templates folder are downloadable in the above URL provided in the book.

However, when I run vsearch4web.py, and I go to my browser and enter the loopback address ( http://127.0.0.1:5000/entry ), I get a "500 Internal Server Error".

Both http://127.0.0.1:5000/ and http://127.0.0.1:5000/search4 work however.

I've tried rechecking the code several times but I don't know what I am missing.

Can someone please help?

Thanks.

-> type syntax is not required in Python.

You should read your server logs to see that def entry_page() is not properly defined.

Use the correct type of render_template (a Response , I think), or just remove it

Learning from the same book... I was stuck on that for a day or two as well. My problem was that I had named the templates with .html but they saved as text rather than html files. To fix it, open your templates, go to "save as..." and check the drop down to see if it says text or html- if they are text, switch to html, and resave.

The following line is causing the problem:

def entry_page() -> 'html':

Using annotations ( -> ) is only possible with Python types and derived (like str , dict , int , float , etc...).

As a matter of fact, you don't even have to use annotations at all in Python:

@app.route('/entry')
def entry_page():
    return render_template('entry.html',the_title='Welcome to search4letters on the web!')

The Response object returned by render_template will have the correct "type", which is determined by the Content-Type: text/html; charset=utf-8 Content-Type: text/html; charset=utf-8 in the response headers (and not by the return value of your route).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM