I am new to flask and web development, I follow a tutorial and I get Internal Server Error. i get the error when trying to access @app.route('question/<title>')
by entering the Url "/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:
<!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.
So can anyone tell me what I missed?
I thought what you want is 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):
...
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.