Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask REST API Resourceful Blueprints Marshalling

Flask-RESTful provides an abort method with custom messages

Hi Kenneth Love, just letting you know that while you can't pass a message keyword argument to Flask's abort method, you can pass it to Flask-RESTful's abort.

So the following would work:

courses.py
from flask import Blueprint
from flask.ext.restful import (Resource, Api, reqparse, inputs, fields,
                               marshal, marshal_with, abort)

import models


course_fields = {
    'id': fields.Integer,
    'title': fields.String,
    'url': fields.String,
    'reviews': fields.List(fields.String)
}


def add_reviews(course):
    course.reviews = [url_for('resources.reviews.review', id=review.id)
                      for review in course.review_set]
    return course


def course_or_404(course_id):
    try:
        course = models.Course.get(models.Course.id==course_id)
    except models.Course.DoesNotExist:
        abort(404, message='Course {} does not exist'.format(course_id))
    else:
        return course

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Ah, I bet that's what happened. When I built the project while writing the lesson, I probably used the abort from flask-restful instead of from flask itself. Good catch!