Flask内部服务器错误

时间:2017-10-02 22:37:43

标签: python flask internal-server-error

我是烧瓶和网络开发的新手,我按照教程进行了内部服务器错误。通过输入网址“/问题/标题”

尝试访问@app.route('question/<title>')时出现错误

代码:

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>'

我从这里得到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>

该应用程序只是一个问答应用程序,我在Jinja模板文件夹中有四个HTML文件,它应该从用户输入中获取标题并将其替换为函数参数,因此如果我输入“python”作为标题Url必须成为“/ question / python”,这就是我想要做的。

那么有谁能告诉我我错过了什么?

1 个答案:

答案 0 :(得分:0)

我认为你想要的是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):
    ...