简体   繁体   English

Flask可插入视图错误:“未实现错误”

[英]Flask Pluggable Views Error: “Not Implemented Error”

I am learning how to use Pluggable Views in Flask, since it seems that everyone uses them always for their advantages. 我正在学习如何在Flask中使用可插入视图,因为似乎每个人都总是使用它们来发挥自己的优势。 I have the following file which returns an "Not Implemented Error". 我有以下文件返回“未实现错误”。 I am assuming that is because I am not implementing the dispatch_request . 我以为那是因为我没有实现dispatch_request However, according to Flask's documentation, when using MethodView: "...if you implement a method called get() it means you will response to 'GET' requests and the dispatch_request() implementation will automatically forward your request to that." 但是,根据Flask的文档,使用MethodView时:“ ...如果实现名为get()的方法,则意味着您将响应'GET'请求,而dispatch_request()实现将自动将您的请求转发给该方法。 Meaning, I do not require dispatch_request . 意思是,我不需要dispatch_request

from flask import Flask, render_template, request, redirect, url_for, flash
from flask.views import View, MethodView
import os

SECRET_KEY = 'some_secret_key'
DEBUG = TRUE

app = Flask(__name__)
app.config.from_object(__name__)  

class Main(View):
  def dispatch_request(self):
    return "Hello World!"

class Template(View):
  def get(self):
    return render_template('index.html')

  def post(self):
    result = eval(request.form['expression'])
    flash(result)
    return self.get()

app.add_url_rule('/', view_func=Main.as_view('main'))
app.add_url_rule('/template', view_func=Template.as_view('template'), methods=['GET', 'POST']) 

if __name__ == "__main__":
  app.run()

Oops.. silly Python beginner's mistake by me. 糟糕,..愚蠢的Python初学者的错误是我的。

I was subclassing flask.views.View instead of flask.views.MethodView . 我在flask.views.View而不是flask.views.MethodView flask.views.View requires dispatch_request , and does not automatically forward HTTP requests to dispatch_request as MethdoView does, hence the error. flask.views.View需要dispatch_request ,并且不会像MethdoView一样自动将HTTP请求转发到dispatch_request ,因此会出现错误。

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

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