0

I'm trying to using Flask Blueprint to make routes from another file. This is my "catalog.py file:

from flask import Blueprint, make_response, render_template
from models.catalog import Catalog

catalog_url = Blueprint('catalog_url', __name__)

@catalog_url.route("/")
@catalog_url.route("/catalog")
def show_all_catalogs():
    try:
        catalogs = Catalog.find_all_catalogs()
        return render_template('publiccatalogs.html', catalogs=catalogs)
    except:
        return {'message': 'Internal Server Error'}, 500

This is my "app.py" file:

from catalog import catalog_url
app = Flask(__name__)
app.register_blueprint(catalog_url)

But when I enter "localhost:5000/catalog", it returns this error:

TypeError: 'dict' object is not callable

I tried returning plain text and even JSON, they worked just fine. But when I try to use "render_template", I keep getting the same error.

An Nguyen
  • 383
  • 1
  • 3
  • 12
  • On which line does the error happen? – moritzg May 19 '17 at 08:05
  • It's happen on: "return render_template('publiccatalogs.html', catalogs=catalogs)". When i replace that with "return 'Hello World'!" or "return jsonify(...catalogs...)" it worked just fine @moritzg – An Nguyen May 19 '17 at 08:12
  • 1
    I think it rather happens on `return {'message': 'Internal Server Error'}, 500`, are you trying to return JSON ? Because that's not the way to do it. – polku May 19 '17 at 08:15
  • oh yes you're right, I should have "jsonify" before that. But I'm trying to say that the error is definitely due to the "render_template" :( – An Nguyen May 19 '17 at 08:24
  • Well, if an exception is raised by the `render_template`, your except code will raises another one that hides the first. That's why 'catch all except' like this are a bad idea. – polku May 19 '17 at 08:28

1 Answers1

0

There is try except, your code return render_template('publiccatalogs.html', catalogs=catalogs) is correct. However, I suspect the html template cannot be found, and then this exception happen, it goes to except return {'message': 'Internal Server Error'}, 500 this part format is not correct.

In Flask, a view must return one of the following:

  • a string
  • a Response object (or subclass)
  • a tuple of (string, status,headers) or (string, status)
  • a valid WSGI application
Tiny.D
  • 6,466
  • 2
  • 15
  • 20