app-generator / flask-adminlte

AdminLTE Flask - Open-source Seed Project | AppSeed
https://appseed.us/product/adminlte/flask/
Other
294 stars 98 forks source link

Enhancement - Flask MongoDB Integration #2

Closed edurguti closed 3 years ago

edurguti commented 4 years ago

Hi, thank you for this template, I am new to flask and I find it confusing on how the app config is setup, I am trying to add mongodb to my app but having a hard time sending the config URI via PyMyngo

app.config["MONGO_URI"] = "mongodb://uv-mongodb:27017/platform")
mongo = PyMongo(app)

I'm getting the following error: AttributeError: module 'app' has no attribute 'config' I'm struggling to send the uri to PyMongo, i've tried as a positional argument but still failing. Any help is apprecisated.

app-generator commented 4 years ago

Hello @edurguti,

MONGO_URI variable should be added in config.py file.

The app has a dual config Development and Production. To make sure your MONGO_DB URI is loaded, add the variable in class Config (the parent class for all configurations):

# contents of config.py

import os
from   decouple import config

class Config(object):
    ...

    # Set up the App SECRET_KEY
    SECRET_KEY = config('SECRET_KEY', default='S#perS3crEt_007')

    MONGO_URI = "mongodb://uv-mongodb:27017/platform")  # <--- Your code HERE

    ....

Let me know if this helps.

edurguti commented 4 years ago

Hi, thanks for your prompt reply, I did add the MONGO_URI variable, however looks like PyMongo is always trying to get the url from the app.config attribute, I did pass the app like so: mongo = PyMongo(app)

File "/Users/edondurguti/Desktop/flask-dashboard-adminlte/app/home/routes.py", line 15, in <module>
    mongo = PyMongo(app)
  File "/Users/edondurguti/Desktop/flask-dashboard-adminlte/env/lib/python3.7/site-packages/flask_pymongo/__init__.py", line 110, in __init__
    self.init_app(app, uri, *args, **kwargs)
  File "/Users/edondurguti/Desktop/flask-dashboard-adminlte/env/lib/python3.7/site-packages/flask_pymongo/__init__.py", line 139, in init_app
    uri = app.config.get("MONGO_URI", None)
AttributeError: module 'app' has no attribute 'config'
app-generator commented 4 years ago

Before using the starter, please take a look at the reference docs. AppSeed (internal automated workflow) generates apps using flat HTML on top of a single codebase for each category. I mention this because all flask dashboards share the same codebase, .. similar to flask apps, eleventy .. etc.

Now back to this starter, the codebase is documented here: Flask Dashboard Boilerplate

Just to help you start faster, the app is constructed using app-factory pattern. The bootstrap flow is:

The run.py finish to construct the app in line 28:

....
app = create_app( app_config ) 
Migrate(app, db)

....

To move forward with your development, try to update run.py as below:

....
app = create_app( app_config ) 
Migrate(app, db)

mongo = PyMongo(app)  # <-- Your code here, where the APP object has a valid config
....

Please note that the codebase is not enhanced with Mongo support, this will be added in the coming weeks. Our suggestions are just hints and is strongly recommended not to be used in production without real tests.

In case you are using Discord, feel free to join the community.

edurguti commented 4 years ago

Hello, the above worked, thank you! Just having issues import the mongo variable to routes.py (this is where I have to use it. I'll fiddle around and see what I can do, appreciate the help and sorry for the n00b questions.

app-generator commented 3 years ago

Flagged as closed.