0

I have a small rest application with the following structure

Basedir
  - manage.py
  - load_gen
    - __init__.py
    - app.py
    - models.py

code snippet from manage.py

from load_gen import app, db
from load_gen.models import User
from flask.ext.script import Manager

manager = Manager(app)

if __name__ == '__main__':
    manager.run()

code snippet from init.py

import os

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db')
db = SQLAlchemy(app)

from load_gen import models
from load_gen import app

and finally app.py

from flask import Flask, jsonify, abort,make_response,url_for    
from models import User

app = Flask(__name__)

tasks = [
    {
        'id': 1,
        'title': u'Buy groceries',
        'description': u'Milk, Cheese, Pizza, Fruit, Tylenol', 
        'done': False
    }
]
def make_public_task(task):
    new_task = {}
    for field in task:
        if field == 'id':
            new_task['uri'] = url_for('get_task', task_id=task['id'], _external=True)
        else:
            new_task[field] = task[field]
    return new_task

@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
    return jsonify({'tasks': [make_public_task(task) for task in tasks]})

if __name__ == '__main__':
    app.run(debug=True)

When I try to run this with python manage.py runserver, my app route is not registered and I keep getting 404 Not Found. url used - localhost:5000/todo/api/v1.0/tasks.

I have spent last 3 hours trying to figure out but am stuck. what am i doing wrong?

Abgo80
  • 233
  • 1
  • 3
  • 9
  • I don't think `app` in `manage.py` is your app, but the module instead (due to the last couple of lines in `__init__.py`). That could be the issue, though I'm surprised you're not running into other issues. – John Szakmeister Dec 17 '15 at 22:59
  • you have app = Flask("app") in init.py (__init__.py ?) and app = Flask(__name__) in app.py. it's strange also – Gerard Rozsavolgyi Dec 17 '15 at 23:05
  • app in manage.py prints "". Its loaded from __init_.py. Can you tell me what else I need to use there? – Abgo80 Dec 17 '15 at 23:07
  • @Gerard Rozsavolgyi - I was testing it. I changed it Flask(__name__). Will edit the code. – Abgo80 Dec 17 '15 at 23:08
  • You have three things named `app`. First you create an `Application` instance in `__init__.py`. Then you import a module named `app`, shadowing the instance. Your third `app` is the instance inside `app.py`. The two instances are going to going to give you problems, especially when the first gets shadowed by a module. – dirn Dec 17 '15 at 23:45
  • @dirn - I changed app.py to load_app.py. Still nothing. I tried this helper [link](http://flask.pocoo.org/snippets/117/) and got following results `static HEAD,OPTIONS,GET /static/[filename]`. I believe my manager is not somehow registering my flask app. – Abgo80 Dec 18 '15 at 01:37

2 Answers2

1

The app you run and the app with which you register your routes are not the same.

In manage.py you have

from load_gen import app

manager = Manager(app)

In app.py (now load_app.py) you have

app = Flask(__name__)

@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
    return jsonify({'tasks': [make_public_task(task) for task in tasks]})

You need to use the same app everywhere. Don't define app in load_app.py and you should be all set, just remove the import at the end of __init__.py and change load_app.py to have

from load_gen import app
dirn
  • 19,454
  • 5
  • 69
  • 74
0

Your manage.py is setting up things in a strange way. I would put some of that code at the end of __init__.py. After you import your models and the views (in app.py), create your manager, at the bottom of init.py:

manager = Manager(app)

if __name__ == '__main__':
    manager.run()

And then in your manage.py, all you have to do is this:

from load_gen import manager
manager.run()

Because the manager object is created inside the application module, and therefore has the views and database created in the proper context.

Aaron D
  • 7,540
  • 3
  • 44
  • 48