Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting django and dj-database-url for local development

Here in comments to the answer, somebody asked this questions. But answer is still unclear for me .

I run my django site in Herouku and it requires dj-database-url module to work with Postgresql. How to run django with DATABASES = dj-database-url() in settings.py on my local computer? Changing code before pushing to Heroku is a pretty ugly way.

like image 406
AlexanderLedovsky Avatar asked Dec 05 '12 18:12

AlexanderLedovsky


People also ask

How do I use a DJ database URL?

"database url" in default string have the value postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...] Show activity on this post. Use pip module dj-dtabase-url. Add an environmental var with the name 'DATABASE_URL' and the value from the heroku db settings.

Can we connect to database Django?

By default, Django works with SQLite, database and allows configuring for other databases as well. Database connectivity requires all the connection details such as database name, user credentials, hostname drive name etc. To connect with MySQL, django. db.


1 Answers

There are many ways to handle different production / development environments.

One is to have a local settings file that's imported at the bottom of your settings file that's not in version control, and thus not in heroku.

Another is any way to distinguish heroku environment from your local environment. An arbitrary environment variable, for example.

Another, is the default argument passed to dj_database_url which basically does this simple if statement for you.

import dj_database_url
DATABASES['default'] = dj_database_url.config(
    default='sqlite:////path-to-my/database.sqlite')

Remember, this settings file is just python. You could have it use one database on Tuesday for example.. any if statement you can come up with will work.

like image 181
Yuji 'Tomita' Tomita Avatar answered Sep 28 '22 10:09

Yuji 'Tomita' Tomita