One thing I would love to see added to the startproject template is the use of DJ_DATABASE_URL for the database settings. I suspect it hasn’t been added since it’s a 3rd party library, and if so, that’s OK.
If we do add it, I use the following code in settings to keep the defaults to SQLite easy:
Hallo!
I like Django too/ I have been working with him only 2 or 3 years.
Now, i understand - what to do this thedj_database_url.parse if we have the remote db.
But , if we have the local db/ Why do we need the dj_database_url.conf ?
What is the plus from him?
We can write the code below
# abstract example
try:
import psycopg2
conn = psycopg2.connect(DATABASE_URL, connect_timeout=3)
conn.close()
except Exception as e: # When we have been a conflict of above
SQLITE_DB_PATH = BASE_DIR / ‘db.sqlite3’
SQLITE_CONFIG = {
‘ENGINE’: ‘django.db.backends.sqlite3’,
‘NAME’: str(SQLITE_DB_PATH),
}
DATABASES = {'default': SQLITE_CONFIG}
The code I showed could be used in all Django projects. If you don’t have the environment variable “DJ_DATBASE_URL” set, it would default to SQLite. If you do have it set, then you’d need whatever python libraries it requires, for example, the postgres libraries.
I think it makes it easy to use SQLite for local development, but then in deploying to production, I don’t need code like yours or other logic to use my production DB.
You work and you can understand it.
i don’t understand. How does it work if they have together (.conf and .parse) ?
If production have two db/ It is the basis db (default) and else db (example postgres). And what? It could be into server.
I too install dj-database-url on most of my Django projects. Having some way of parsing database URIs natively would be quite nice. django-environ is another package which does similar.
For suggesting new features to Django (or at least including an external package), I’d recommend posting your suggestion on the new-feature repo. It’s a good place to voice the suggestion and get community feedback!
I like SQLite for Dev and Postgres for production, with my code example, I only need to define the DATBASE_URL environment variable in production, otherwise it defaults to SQLite.
@jfmatth I understand you in the your question about the environment. Ok.
SQLite and PostgreSQL i voiced as an example. Ok/
I want to know - Where do you use this library -dj_database_url
Did i understand you correctly - what is it (dj_database_url) used only for develop mode?
OK
I can see the benefit of this library (dj_database_url ) only if i will can write some code.
Example
import os
import dj_database_url
# .... and some import by django 's default
DEFAULT_SQLITE = f"sqlite:///{BASE_DIR / 'db.sqlite3'}"
def build_postgres_url_from_env():
name = os.getenv("DB_name", "")
if not name:
return False
user= os.getenv("DB_USER", "")
password = #.....
hos = # ....
port = # ...
# Below, we build him.
auth = f"{user}:{password}@" if user and password else ""
return f"postgres-async://{auth}{host}:{port}/{name}"
# then :
# if we have a working DATABASE_URL it mean what we use it/
# or we can use the postgresql
# other the sqlite.
# Note: This means, our project has three db and they is to the production mode.
database_url = os.getenv("DATABASE_URL ") or build_postgres_url_from_anv() or DEFAULT_SQLITE
DATABASE = {
'default":dj_database_url.config(
default=database_ur,
conn_max_age=600
)
}
Ebove, means - We or our user have anything from db and oonly the 4 line/row of options.
HI @Tryd0g0lik I think you’re making this too complicated.
In Django, without DJ_DATABASE_URL, I would have to modify the code for DATABASES= settings. With DJ_DATABASE_URL, I just have to set and environment variable DATABASE_URL
The examples on the DJ website don’t do it justice. You only need a simple environment variable to have it setup the DATABASES setting.
For dev with the line I showed, it would be just like not modifying settings.py at all. For other environments, I define a DATABASE_URL environment variable, DJ_DATABASE_URL library picks that up and it works, simple as that.