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

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.

Related

Can't Connect Python Flask With PSQL Database

every time i try to make connection between python and PSQL this error disappear ?
You need to first make sure your DATABASE_URL has been defined before you even run your server. If you are using an online database and have a url, executing the command in terminal (for mac/linux):
export DATABASE_URL = "https://my_database_link"
inside your virtual environment, where you installed python will connect your virtual environment variable to your database online. If you are still experiencing problems. make sure you have installed everything correctly.

Unable to migrate Django db when using docker container

On my Windows 10 machine, I am developing a database manager. Because the backend uses LDAP and the required development libraries are only available for Linux, I want to use Docker to set up an environment with the appropriate libs.
I managed to write a Dockerfile and compose file, that launch the (currently very basic) Django app in a Docker container with all the libs necessary.
I would like to play around with the django-ldapdb package and for that I want to apply the migrations.
When I open PyCharm's terminal and try to execute python manage.py migrate, I get an error telling me that the module ldapdb is not found. I suspect this is because the command does not use the remote Docker interpreter I set up with PyCharm.
The other thing I tried is using PyCharm's dedicated manage.py console. This does not initialize properly. It says the working directory is invalid and needs to be an absolute path, although the path it shows it the absolute path to the project.
I have to admit that I have no idea how this remote interpreter works and I don't see any Docker container running, so I might have not understood something properly here. I even tried running the app using PyCharm's Django run config, which started a container, but still I get the same errors.
I googled a lot, but I couldn't find more infos about remote interpreters nor something solving my issue.
The only way I managed to do this, is by executing the command inside the container.
To get inside a container named contr, use the docker command
docker exec -ti contr /bin/bash

Making a django application running on 127.0.0.1:8000 accessible everywhere?

I only have ssh access to a server. It is ubuntu from amazon aws. I have uploaded my django project to it. When I run the command python manage.py runserver or any variants of it, the server starts.
But as you know this will not allow me to access the web application
from my browser which resides on a different network.
My question is
What would be the way that after I give my command to runserver either via django's development server or gunicorn. I may access it from the outside world by typing IP:8000 in my browser.
Secondly I would like to turn the access to IP:8000 on/off as needed. Like I may allow access for 5 minutes and then run a command to stop the access from outside world?
For accessing the django project else every where you have use the command
python manage.py runserver 0.0.0.0:8000
then You will be access it outside by giving the IP:8000
Note : the server provided by the Django is only for the development purpose only . If you want a permanent hosting you may like to use Apache or any server , where You can host your project. Hope This helps . :)
You need assign a elastic IP address (EIP) on the aws ec2 instance, and allow the inbound traffic 0.0.0.0/0 to port 8000 from its security group.
Then you should be fine to access it from every where with EIP, such as http://54.12.23.34:8000
EIP may generate cost, if you stop the ec2 instance.
You may also want to try the command:
python manage.py runserver [::]:8000
This will bind runserver to all available interfaces on your AWS VM. Please also note: runserver is really just meant for development. Running it in production is a bad idea.
You can use http://localtunnel.me/, it's very very easy but you should use it only for development purposes.
Just in case, you want to run it on 80 port then you need root permissions
sudo python manage.py runserver 0.0.0.0:80

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 debug Django app running on Heroku using a remote pdb connection?

To debug a bug I'm seeing on Heroku but not on my local machine, I'm trying to do step-through debugging.
The typical import pdb; pdb.set_trace() approach doesn't work with Heroku since you don't have access to a console connected to your app, but apparently you can use rpdb, a "remote" version of pdb.
So I've installed rpdb, added import rpdb; rpdb.set_trace() at the appropriate spot. When I make a request that hits the rpdb line, the app hangs as expected and I see the following in my heroku log:
pdb is running on 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc:4444
Ok, so how to connect to the pdb that is running? I've tried heroku run nc 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc 4444 to try to connect to the named host from within heroku's system, but that just immediately exits with status 1 and no error message.
So my specific question is: how do I now connect to this remote pdb?
The general related question is: is this even the right way for this sort of interactive debugging of an app running on Heroku? Is there a better way?
NOTE RE CELERY: Note, I've now also tried a similar approach with Celery, to no avail. The default host celery's rdb (remote pdb wrapper) uses is localhost, which you can't get to when it's Heroku. I've tried using the CELERY_RDB_HOST environment variable to the domain of the website that is being hosted on Heroku, but that gives a "Cannot assign requested address" error. So it's the same basic issue -- how to connect to the remote pdb instance that's running on Heroku?
In answer to your second question, I do it differently depending on the type of error (browser-side, backend, or view). For backend and view testing (unittests), will something like this work for you?
$ heroku run --app=your-app "python manage.py shell --settings=settings.production"
Then debug-away within ipython:
>>> %run -d script_to_run_unittests.py
Even if you aren't running a django app you could just run the debugger as a command line option to ipython so that any python errors will drop you to the debugger:
$ heroku run --app=your-app "ipython --pdb"
Front-end testing is a whole different ballgame where you should look into tools like selenium. I think there's also a "salad" test suite module that makes front end tests easier to write. Writing a test that breaks is the first step in debugging (or so I'm told ;).
If the bug looks simple, you can always do the old "print and run" with something like
import logging
logger = logging.getLogger(__file__)
logger.warn('here be bugs')`
and review your log files with getsentry.com or an equivalent monitoring tool or just:
heroku logs --tail

Categories

Resources