I am developing a Django app but unfortunately my laptop is broken and will take a month to get repaired. So mean while I am using a android app called Termux to work on the project. But Termux still doesn't have a postgresql package and don't want to mess my settings just for running the project. So how can I use dj_database_url packages to configure my databases using database urls??
This can be done using the dj_database_url module.
After installing the module in the project, then this config needs to be done(example for default database):
db_from_env=dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
Now export a environment variable named DATABASE_URL for the database server
More info here: https://github.com/kennethreitz/dj-database-url/blob/master/README.rst
Related
I'm planning to write an add-on app for an existing django app. The primary app is running smoothly via docker-compose, and I've remoted into the container via the vscode remote-containers extension.
Add-ons are installed like most any other django app, pip install APPNAME, append to INSTALLED_APPS, migrate, collectstatic, restart server.
I can create a directory anywhere in the container, where the add-on will exist and be developed, but I'm looking for a way to make the app visible from that directory so I can include it in the primary app's INSTALLED_APPS list. Is this possible, or would I have to create the app inside the primary package's site-packages directory alongside its own sub-apps for this to work?
I have a flask app that uses a different database based on production vs development environment variables. I am worried about a developer forgetting to set FLASK_ENV=development before running their local flask app, and suddenly they are making updates to a production database.
My only easy solution I have thought of is restricting the production DB to only accept requests from the production server IP so that way everything will error out if the developer forgets to set the environment variable, but I was wondering if there are better solutions for this issue.
First of all, it is a good practice to limit access to your production database to trusted IPs only.
As you can read in Configuration Handling: Development/Production (Flask Docs), you can have multiple configurations and use inheritance.
class Config(object):
DATABASE_URI = 'sqlite:///:memory:'
class ProductionConfig(Config):
DATABASE_URI = 'mysql://user#localhost/foo'
class DevelopmentConfig(Config):
pass
You can load, all the time, the default configuration that is safe. Only if the production environment variable is set, the real database configuration will be loaded.
Another solution is to use the instance folder (Flask Docs) that mustn't be a part of your git repository.
The instance folder is designed to not be under version control and be deployment-specific.
So, when you deploy your app, just add your production configuration to this instance folder, and nobody would have the prod configuration on their local machine.
They have a few examples and explain very well how to use it in the link that I gave you above.
You can prewrite the environment variables in .flaskenv:
FLASK_ENV=development
Then install python-dotenv:
pip install python-dotenv
Now if you run your application locally with flask run, Flask will automatically read the .flaskenv and set the environment variables in it.
I'm trying to deploy my Django app to Google App Engine (GAE) as per this document. I created and configured a Google Cloud SQL instance, as described in that document. I use PyCharm as development environment and created a GAE project with Django support.
I configured a local server to point to the GAE server. When I try to launch the GAE local server in PyCharm, it's raising exceptions on an improperly configured database in SETTINGS.PY:
google.appengine.ext.django.backends.rdbms' isn't an available database backend
I can see from the stack trace that the local server is using the Django version in /Library/Python/2.7/site-packages while I presume it should use the one in /usr/local/google_appengine/lib.
What would be the best way to solve this given that I have other Django projects as well that should use the Django version in /Library/Python/2.7/site-packages? If I modify my PYTHONPATH to include the GAE version of Django, would not all my projects be referencing that version of Django?
EDIT: To be more precise, the GAE local server starts just fine but throws the mentioned stack trace when I do a syncdb task to update my database.
EDIT 2: In PyCharm Settings under Python Interpreter, I found the possibility to modify paths and added the Django 1.4 version as distributed with GAE SDK. When I start the GAE development server, I can actually see it uses the Django version from the GAE SDK but it still crashes on the database definitions:
Error was: No module named google.appengine.ext.django.backends.rdbms.base
EDIT 3: I ran into problems when trying to deploy an existing Django app using the tutorial. See this separate question.
Looks like PyCharms call of syncdb is using the wrong Django installation.
google.appengine.ext.django.backends.rdbms is not part of the official Django distribution, but it is part of GAEs django.
My GAE django is in /usr/local/google_appengine/lib/
If you're on linux/OS X you could add this to your .bashrc/.bash_profile and make syncdb use this:
export GAE="/usr/local/google_appengine"
export PYTHONPATH="$PYTHONPATH:$GAE:$GAE/lib/django_1_4"
export PATH=${PATH}:$GAE/lib/django_1_4/django/bin/
export PATH=${PATH}:/usr/local/mysql/bin
I wrote a tutorial about using Django with GAE and Google Cloud SQL. There might be some relevant infos there as well.
I need to setup a Django dev environment.
I did a git clone and pulled all the django project files from production on my local machine (a Vagrant enabled VM).
The problem is that my local machine has a different path to the project than my production ( and I can't change that) so it's having problems finding modules stated under INSTALLED_APPS on my local machine.
For example on the production my project is on the /myproject folder while on my local machine is under /vagrant/web/myproject.
On the production I'm accessing my app modules like this:
INSTALLED_APPS = ( 'myproject.myapp')
Also within the Django apps I'm accessing various app modules like this:
from myproject.myapp.models import *
What do I need to do to emulate production paths to my modules on my dev box so I don't have to change the paths to the modules on my local machine?
If you're doing project-relative imports, all you need to do is ensure that the path directly above your project is on the PYTHONPATH.
You need only issue the following at the command line:
export PYTHONPATH='/vagrant/web'
If you're using virtualenv, you can add that line to your environment's bin/activate file.
The path to your application directory should not matter there as long as you dont have hard-coded paths in settings.py, what web server are you using?
In your settings.py:
import os
prj_root = os.path.realpath(os.path.dirname(__file__))
And prj_root will be path to your root project folder
I followed the wiki on http://wiki.dreamhost.com/Django .
Django is installed in my site's directory and I've even created my first project, now what do I do ?
You can copy your project files to the server and transfer your database to the sql server on dreamhost. Don't forget to make changes in your settings.py to reflect the environment. Should work otherwise me thinks.