Python瓶没有返回cookie/ValueError:查看功能没有返回响应

我仍在努力将Python中的字符串传递给另一个函数。Python瓶没有返回cookie/ValueError:查看功能没有返回响应

我试图按照这里给出的例子:https://www.tutorialspoint.com/flask/flask_cookies.htm

我已经试过落实到我的代码,这但现在我得到以下回溯:

Traceback (most recent call last): 

File "/Users/marcel/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app

response = self.full_dispatch_request()

File "/Users/marcel/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1615, in full_dispatch_request

return self.finalize_request(rv)

File "/Users/marcel/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1630, in finalize_request

response = self.make_response(rv)

File "/Users/marcel/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1725, in make_response

raise ValueError('View function did not return a response')

ValueError: View function did not return a response

这将意味着我我没有返回任何东西,但我真的不知道我要去哪里错了,我不得不承认我很迷茫。

我完整的代码看起来是这样的:

from __future__ import print_function 

import tensorflow as tf

import argparse

import os

from six.moves import cPickle

from model import Model

from six import text_type

import flask

from flask import jsonify, render_template, request, make_response

app = flask.Flask(__name__)

@app.route('/')

def index():

return render_template('index.html')

@app.route('/test')

def tester():

return "This is a test"

@app.route('/', methods = ['POST', 'GET'])

def main():

if request.method == 'POST':

parser = argparse.ArgumentParser(

formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('--save_dir', type=str, default='save',

help='model directory to store checkpointed models')

parser.add_argument('-n', type=int, default=500,

help='number of characters to sample')

parser.add_argument('--prime', type=text_type, default=u' ',

help='prime text')

parser.add_argument('--sample', type=int, default=1,

help='0 to use max at each timestep, 1 to sample at '

'each timestep, 2 to sample on spaces')

args = parser.parse_args()

resp = make_response(render_template('sample.html'))

resp.set_cookie('args', args)

return resp

@app.route('/sampler', methods = ['POST', 'GET'])

def sample():

args = request.cookies.get('args')

with open(os.path.join(args.save_dir, 'config.pkl'), 'rb') as f:

saved_args = cPickle.load(f)

with open(os.path.join(args.save_dir, 'chars_vocab.pkl'), 'rb') as f:

chars, vocab = cPickle.load(f)

model = Model(saved_args, training=False)

with tf.Session() as sess:

tf.global_variables_initializer().run()

saver = tf.train.Saver(tf.global_variables())

ckpt = tf.train.get_checkpoint_state(args.save_dir)

if ckpt and ckpt.model_checkpoint_path:

saver.restore(sess, ckpt.model_checkpoint_path)

if request.method == 'POST':

text = model.sample(sess, chars, vocab, args.n, args.prime,

args.sample).encode('utf-8')

resp = make_response(render_template('text.html'))

resp.set_cookie('verse', text)

return resp

@app.route('/text')

def printer():

verse = request.cookies.get('verse')

return verse

@app.errorhandler(404)

def page_not_found(error):

return render_template('404.html'), 404

if __name__ == '__main__':

main()

之前,我曾试图刚刚从我的样本功能打印机功能发送Cookie,但它返回相同的错误。

我的代码之前:

def sample(args): 

with open(os.path.join(args.save_dir, 'config.pkl'), 'rb') as f:

saved_args = cPickle.load(f)

with open(os.path.join(args.save_dir, 'chars_vocab.pkl'), 'rb') as f:

chars, vocab = cPickle.load(f)

model = Model(saved_args, training=False)

with tf.Session() as sess:

tf.global_variables_initializer().run()

saver = tf.train.Saver(tf.global_variables())

ckpt = tf.train.get_checkpoint_state(args.save_dir)

if ckpt and ckpt.model_checkpoint_path:

saver.restore(sess, ckpt.model_checkpoint_path)

text = model.sample(sess, chars, vocab, args.n, args.prime,

args.sample).encode('utf-8')

resp = make_response(render_template('text.html'))

resp.set_cookie('verse', text)

return resp

@app.route('/text')

def printer():

verse = request.cookies.get('verse')

return verse

回答:

verse cookie的值不存在的价值verse变量将变为None。这意味着您的text视图将返回None,其中Flask不喜欢并理解为“视图函数未返回响应”。

您可能需要妥善处理这种情况下 - 例如,简单地返回一个空字符串会解决这个问题(当然,这只是一个样本简单的解决方法来证明的差异):

@app.route('/text') 

def printer():

return request.cookies.get('verse', '')

而且,并非sample()视图中的所有代码路径都会返回有效的响应 - 请确保解决该问题(例如,向sample()视图发出GET请求会导致相同的“视图函数未返回响应”错误) 。

回答:

main,你将需要外部的请求参数校验范围返回值:

@app.route('/', methods = ['POST', 'GET']) 

def main():

if request.method == 'POST':

parser = argparse.ArgumentParser(

....#statements follow

args = parser.parse_args()

resp = make_response(render_template('sample.html'))

resp.set_cookie('args', args)

return resp

return render_template('home.html') #need to return something here

以上是 Python瓶没有返回cookie/ValueError:查看功能没有返回响应 的全部内容, 来源链接: utcz.com/qa/257538.html

回到顶部