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 Blueprints

Riccardo Candido
seal-mask
.a{fill-rule:evenodd;}techdegree
Riccardo Candido
Python Web Development Techdegree Student 2,790 Points

Unable to run the app after I use 'app.register_blueprint(courses_api)'

I followed everything so far, however I am stuck with Blueprint now. I get this error and I am not even sure what exactly the problem is:

$ python app.py
Traceback (most recent call last):
  File "app.py", line 13, in <module>
    app.register_blueprint(mailbox_api)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 62, in wrapper_func
    return f(self, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 889, in register_blueprint
    blueprint.register(self, options, first_registration)
  File "/usr/lib/python2.7/site-packages/flask/blueprints.py", line 153, in register
    deferred(state)
  File "/usr/lib/python2.7/site-packages/flask/blueprints.py", line 172, in <lambda>
    s.add_url_rule(rule, endpoint, view_func, **options))
  File "/usr/lib64/python2.7/site-packages/flask_restful/__init__.py", line 161, in _blueprint_setup_add_url_rule_patch
    view_func, defaults=defaults, **options)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 62, in wrapper_func
    return f(self, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 976, in add_url_rule
    rule = self.url_rule_class(rule, methods=methods, **options)
  File "/usr/lib/python2.7/site-packages/werkzeug/routing.py", line 541, in __init__
    raise ValueError('urls must start with a leading slash')
ValueError: urls must start with a leading slash

By the way, I changed the name of the variables, but I am sure I am consistent with that and that that is not the issue here.

1 Answer

Anthony Kimberly
Anthony Kimberly
11,049 Points

I had this same problem and was able to find in my courses.py file I was missing the slash ("/") at the beginning of one of the URIs. I had this:

api.add_resource(
    Course,
    'api/v1/courses/<int:id>',
    endpoint='course'
    )

and it should've been this:

api.add_resource(
    Course,
    '/api/v1/courses/<int:id>',
    endpoint='course'
    )

If that doesn't fix it can you post your code?