简体   繁体   English

Flask 内部服务器错误

[英]Flask Internal Server Error

I am new to flask and web development, I follow a tutorial and I get Internal Server Error.我是 Flask 和 Web 开发的新手,我按照教程进行操作,但遇到内部服务器错误。 i get the error when trying to access @app.route('question/<title>') by entering the Url "/question/title"尝试通过输入网址“/question/title”访问@app.route('question/<title>')时出现错误

code:代码:

from flask import flash, url_for, request, render_template
from app import app
import redis

#connect to redis data store
r = redis.StrictRedis(host='localhost', port=6379, db=0, charset='utf-8', decode_responses= True)

#alternate ways to connect to redis, each command is equivalent
#r = redis.StrictRedis()
#r = redis.StrictRedis('localhost', 6379, 0)

# server/
@app.route('/')
def hello():
    #ceating a link to the second page
    create_page = url_for('create') 
    return '<a href="' + create_page + '">Creat Question</a>'

# server/create
@app.route('/create', methods =['GET', 'POST'])
def create():
    if request.method == 'GET':
        # send to user the form
        return render_template('CreateQuestion.html')
    elif request.method == 'POST':
        #read the data and save it
        title = request.form['title']
        answer = request.form['answer']
        question = request.form['question']

        #store data in data store
        r.set(title + ':question', question)
        r.set(title + ':answer', answer)

        return render_template('CreatedQuestion.html', question = question)
    else:
        return "<h2>Invalid request</h2>"


@app.route('/question/<title>', methods = ['GET', 'POST'])
def question(title):
    if request.method == 'GET':
        # send the user the form
        question = r.get(title + ':question')
        return render_template('AnswerQuestion.html', question = question)
    elif requset.method == 'POST':
        # user has attempt answer. cheak if they're correct
        submittedAnswer = request.form['submittedAnswer']

        answer = r.get(title + ':answer')

        if submittedAnswer == answer:
            return render_template('Correct.html')

        else:
            return render_template('Incorrect.html', submittedAnswer = submittedAnswer, answer = answer)

    else:
        return '<h2>Invalid request</h2>'

I get the title from here:我从这里得到title

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Create a new question</title>
</head>
<body>
    <h2>Please create a new question</h2>
    <form method="post">
        <div>
            <label for="title">Title</label>
            <input type="text" name="title" />
        </div>
        <div>
            <label for="question">Question</label>
            <input type="text" name="question" />
        </div>
        <div>
            <label for="answer">Answer</label>
            <input type="text" name="answer" />
        </div>
        <button type="submit">Submit question</button>
    </form>
</body>
</html>

The app is simply a question and answer app, I have four HTML files in Jinja templates folder, it's supposed to take the title from the user input and replace it with the function argument, so if I enter "python" as a title the Url must become "/question/python", that what I trying to do.该应用程序只是一个问答应用程序,我在 Jinja 模板文件夹中有四个 HTML 文件,它应该从用户输入中获取标题并将其替换为函数参数,因此如果我输入“python”作为标题,则 URL必须变成“/question/python”,这就是我想要做的。

So can anyone tell me what I missed?那么谁能告诉我我错过了什么?

I thought what you want is redirect() :我以为你想要的是redirect()

from flask import Flask, redirect, url_for, render_template
...

@app.route('/create', methods =['GET', 'POST'])
def create():
    if request.method == 'POST'
        ...
        return redirect(url_for('question', title=title))  # this line
    ...

@app.route('/question/<title>', methods = ['GET', 'POST'])
def question(title):
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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