Shell command to access db in Flask - python

I'd like to access the database configured in the setting via a shell command in Flask. Is there any equivalent command in Flask to python manage.py dbshell in Django?

No there is no such command exists for flask , but you can access your db with normal db clients

Related

Is there a way to password protect the python shell in a Django application?

I want to password protect the shell when running python manage.py shell from inside my Django app for a superuser.
After running python manage.py shell I would like to be prompted to enter a superusers username and password.
You can create superusers using the createsuperuser command:
$ python manage.py createsuperuser --username=joe --email=joe#example.com
You also can use django.auth.middleware. The django.auth.middleware needs to hide some actions for unauth user on your server.
More information about auth in web request here

Heroku django manage shell with environment setup (db, vars)

I'm trying to get to the django shell so that I can set some superuser permissions for our staff accounts. Doing this by ssh tunneling via ps:exec
We just moved over to heroku, and are using their auto-configured (read: automagical black box) module import django_heroku; django_heroku.settings(locals()) to set database settings.
When I open the shell, those config settings aren't run, and I get an error saying that
django.db.utils.OperationalError: no such table: user
Basically, I can't get the shell instance to use Heroku's nice postgres configurer. Any ideas? Workarounds to manually query accounts and set some params? I've tried to import that same magical module from Heroku as part of the shell and instantiate it, but it only works when run in the settings.py file.
I'm trying to not actually log into the postgres instance so that I don't have to open any IP ranges, etc.
__________edit_________
On a similar note, it looks like the shell also can't load Heroku's env vars... getting some issues there as well. Is there a way to run the shell with Heroku's environment completely?
You can run python manage.py shell directly through the heroku cli:
heroku run python manage.py shell --app=your-app-name
You can access the Django shell through the Heroku Dashboard by using the view console button.
So, you can access your shell (Django shell) or the Linux shell of Heroku directly.

Flask can't see sqlite db when launched via uWSGI Emperor

When I run my Flask app via uWSGI emperor, it 502's out with a Sqlite error regarding it not being able to see my tables. I can go in via the sqlite3 command and verify the data is there. When I run the site via
uwsgi --ini site_conf.ini
it works just fine, but not via emperer.
Check you are not using relative paths when referring to the sqlite db. When run by the Emperor the cwd changes to the vassals dir. Eventually use chdir option in your vassal to move to a specific directory

How To Run Postgres locally

I read the Postgres docs for Flask
and they said that to run Postgres you should have the following code
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = postgresql://localhost/[YOUR_DB_NAME]'
db = SQLAlchemy(app)
How do I know my database name?
I wrote db as the name - but I got an error
sqlalchemy.exc.OperationalError: (OperationalError) FATAL: database "[db]"
does not exist
Running Heroku with Flask if that helps
First step, is to get Flask + Postgresql running locally, and the first step to do that is to install postgresql on your machine. Next, you should install the python drivers for postgresql.
For Windows, you can use the windows installer for postgresql and the windows installer for the python drivers.
Once you have finished the above two steps, you need to create a database.
For Windows, you can use the bundled pgadminIII tool. Here is a video that shows how to do just that.
For example, here is how you create a database called the_database, and create a user called databaseuser with a password P#ssw0rd using the built-in tool psql:
$ psql -d template1 -U postgres
template1=# CREATE USER databaseuser WITH PASSWORD 'P#ssw0rd';
template1=# CREATE DATABASE the_database;
template1=# GRANT ALL PRIVILEGES ON DATABASE the_database to databaseuser;
template1=# \q
Here is how you would configure your application with the above information:
db_conn = 'postgresql+psycopg2://databaseuser:P#ssw0rd#localhost/the_database'
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = db_conn
db = SQLAlchemy(app)
I do not have experience with Heroku, but from your messages I would say you don't have PostgreSQL running and you dont have a database at all. Looks like here they have some info https://postgres.heroku.com/ :)
But in your topic you asking about localhost PostgreSQL... So if you want to run your setup locally, first thing to check is if you have PostgreSQL installed and running. If you do, try to connect to it with pgAdmin or from console and create database. If you can't connect - check config for ports, hosts PostgreSQL is listening.
Anyway those messages I see you posted in question in comments, gives me only the idea that you don't have DB created or even running (or you have wrong config).

which server did I use,when I configure django admin tool after typing "python manage.py runserver"?

I am configuring django for using django admin tool, following steps in webpage http://www.ibm.com/developerworks/linux/library/l-django/?S_TACT=105AGX52&S_CMP=cn-a-l
which server did I use,when I type "python manage.py runserver"? apache? or a simple http server in python?
And I have apache 2.2 listening port 80.
You are using Django's development server, which as you say is a simple http server written in Python, and runs in the Python process you start with the "python manage.py runserver" command. Note that this server is not meant for production use. For example, it can handle only one request at a time.

Categories

Resources