Answer the question
In order to leave comments, you need to log in
TypeError: The view function did not return a valid response.The function either returned None or ended without a return statement how to solve in Flask?
python code
from flask import Flask, render_template, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///poem.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Ivan(db.Model):
id = db.Column(db.Integer, primary_key = True)
author = db.Column(db.String(100), nullable = False)
namepoem = db.Column(db.String(200), nullable = False)
poem = db.Column(db.Text, nullable = False)
date = db.Column(db.DateTime, default = datetime.utcnow)
def __repr__(self):
return '<Ivan %r>' % self.id
@app.route('/')
def index():
return 'Hello World!'
@app.route('/create-poem/', methods=['POST', 'GET'])
def create():
if request.method == 'POST':
author = request.form['author']
name = request.form['namepoem']
poem = request.form['poem']
ivan = Ivan(author=author, namepoem=name, poem=poem)
try:
db.session.add(ivan)
db.session.commit()
return redirect('/')
except:
pass
else:
return render_template('create.html')
app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Добавление поэмы или стихотворения</title>
</head>
<body>
<h1 style="text-align: center;">Добавление</h1>
<form method="POST">
<input type="text" name="author" id = "author" class="form-control" placeholder="Автор"></br>
<input type="text" id="namepoem" name="namepoem" class="form-control" placeholder="Название произведения"></br>
<input type="text" id="poem" name="poem" class="form-control" placeholder="Произведение"></br>
<input type="submit" class="btn btn-success" value="Отправить">
</form>
</body>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</html>
Answer the question
In order to leave comments, you need to log in
The route should always return something, but in case of an error, it does not return anything in your case. Moreover, due to the fact that you decided to just "silence" the error, you do not know why you have this error, but you could do like this:
import logging
@app.route('/create-poem/', methods=['POST', 'GET'])
def create():
if request.method == 'POST':
...
try:
...
except Exception:
logging.exception('')
return ''
else:
...
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: ivan
db.create_all()
app.run(debug=True)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question