darksky
darksky

Reputation: 21069

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. I have the following file which returns an "Not Implemented Error". I am assuming that is because I am not implementing the 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." Meaning, I do not require 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()

Upvotes: 1

Views: 1812

Answers (1)

darksky
darksky

Reputation: 21069

Oops.. silly Python beginner's mistake by me.

I was subclassing flask.views.View instead of 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.

Upvotes: 1

Related Questions