I have database environment variables specified for my django app:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.getenv("POSTGRES_NAME"),
'USER': os.getenv("POSTGRES_USER"),
'PASSWORD': os.getenv("POSTGRES_PW"),
'HOST': os.getenv("POSTGRES_HOST"),
'PORT': os.getenv("POSTGRES_PORT"),
}
}
The variables successfully get read in when I run "python manage.py runserver", during the build on Circle CI, and also in its production environment. But I am not understanding why when I run unit tests they don't get read in.
Thanks for the help!
Turns out the problem was that I hadn't closed my IDE in some time. I had to reboot the IDE to source the env vars from the virtual environment.
Related
In mysite.settings.py I have:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.path.join(BASE_DIR, 'test'),
#'NAME': 'test',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
}}
When I run 'python manage.py migrate' in the PyCharm terminal, nothing happens, no error message etc. No new tables are created in the database specified. I tried two things for NAME as you can see.
MySQL Server version 8.0.19
MySQL Connector/Python 8.0.19
Python version 3.8
In PyCharm I have package mysqlclient 1.4.6 installed.
Any advice on how to find the problem? How to get an error message?
Thank you
DATABASES property specifies your DB configurations. Now NAME is the property which specifies your database name.
Currently here 'NAME': os.path.join(BASE_DIR, 'test'), won't work as it will give you something like C:/YourUser/YourPath... so this string is not a valid db name. basically (os.path.join() returns a path to your project directory)
First create a db manually on MySql, note the name and put it in property as 'Name': DBNAME in SETTINGS.py.
(Make sure you have your environment set while running following commands)
Then try python manage.py makemigrations for checking migration changes, if everything goes fine up till here then fire python manage.py migrate command.
I'm trying to make the migrations between my database (MySQL) and Django. I used the same parameters in Linux and didn't have any problems. Now I'm using the command :
python manage.py migrate
and I get nothing at all on the terminal. However, the command works if I let the default parameters ( for a sqlite database ). Also, I've noticed that it actually reads the 'settings.py' file because it returns an error if I write something that doesn't make any sense. Here are my parameters, I know for sure that they are correct ( I checked with the MySQL commands ).
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_db',
'USER': 'root',
'PASSWORD': '123456789_dont_judge_me',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
Error message
I'm using Windows 10, Python 3.5 and Django 2.0.
Do you have any idea where the problem could come from ?
Thank you for your responses kind people !
You can try this.
Note: I am not claiming it is best solution, but it is working for me
in case of Window10.
Step1 : Install pymysql
pip install pymysql
Step2: Edit settings.py
settings.py
---------------
import os
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb
# Database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_test',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
Step3: migrate it
python manage.py migrate
you can follow this link
I have built a simple Django app and successfully deployed it to Beanstalk. The app uses a PostgreSQL backend on an RDS instance. From a browser, I can successfully access the admin and create and delete models inside of it. However, I'm also trying to run a cron that updates the database. I installed the cron on the server, but it didn't work. So I then shelled in, ran the commands manually and got the following error: Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?
From my Googling, I'm guessing this has something to do with either security groups, allowed hosts or JDBC. Perhaps allowing the Beanstalk's EC2 instance and RDS instance to interactive with each other. But I'm lost. I tried the instructions from this AWS tutorial.
For the record, the script that the cron runs works perfectly when run locally as python manage.py runscript scrape.
Other stuff:
The tutorial I followed for deploying my app.
The tutorial I followed for the cron
Cron
* * * * * /opt/python/run/venv/bin/python3.4 /opt/python/current/app/manage.py runscript scrape
Database part of settings.py
if 'RDS_DB_NAME' in os.environ:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db',
'USER': 'user',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '',
}
}
This is the way you run a python script.
It is a question on cron.
First is add a SHEBANG line on top of your python script.
#!/usr/bin/env python3
Make your script executable with chmod +x
And do a crontab -e and add 0 0 */2 * * /path/to/your/pythonscript.py
I'm dumb. I forgot to actually set the environment variables in the AWS console. What can I say, it has been a long day.
I just downloaded a newer version of MAMP (3.2.1) and i noticed that this Version has Python installed and also seems to handle SQLite Databases.
Shouldn't I be able to manage Django Projects with it?
Where and how would i install it?
I found some Posts in the Web (before my new MAMP release) where People already trying to get MAMP + Django to work with MySQL but those seemed more complicated to me then the usual setup with Virtualenv + SQLite/Postgres.
I'm pretty new to django but starting a project at the time seems quite simple to me.
If Django would work with MAMP together what would be the advantages?
Anyone has already experiences or useful links?
OK i gues working with MAMP MySQL has the advantage that i can easy import/export Database with php MyAdmin tool.
Anyway based on tanorix answer here how for me Django worked with MAMP MySQL Database:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'projectdb',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',
'PORT': '8888',
}
}
Then
python manage.py migrate
I don't have knowledge about MAMP but I can give you some elements to put Django Database with WAMP, so I think it can be the same manipulation:
First, in MAMP, you need to create a database, call it : projectdb.
Then, at your settings.py, update your variable DATABASES like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'projectdb', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
Then, if you are using South, at your shell write this:
python manage.py schemamigration <name of your app> --init
python manage.py syncdb # => create your tables at your MAMP
python manage.py migrate
I have a project that works on Heroku, I don't have PostgreSQL installed on my local machine. I want to keep running the app on my local machine using sqlite3, but when I push it to Heroku it will convert to pg
All I am trying to do is to have an IF condition if this is development then run sqlite3 .. but if it's production run then following command.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '',
'HOST': '',
'PORT': 5432,
'USER': '',
'PASSWORD': ''
}
}
Heroku is working with dj_database_url
import dj_database_url
DATABASES['default'] = dj_database_url.config()
It basically similar to Rails when we define the gems for production and another gems for testing and development.
You can create a local_settings.py file inside your project, and import it from your base setting file. This way you can have different settings for each environment.
This local_setting file should be included in your .gitignore
Do you use VirtualEnvs?
You can setup the settings.py like:
DATABASES = {
'default': {
'ENGINE': get_var('DB_ENGINE'),
'NAME': get_var('DB_NAME'),
'HOST': get_var('DB_HOST'),
...
}
}
where get_var is reading the environment variables. These envvars are set by your virtualenv's postactivate file.
The production virtualenv postactivate sets the envvars DB_ENGINE, DB_NAME, DB_HOST with postgresql values
The dev virtualenv postactivate file sets env vars corresponding to the development DB.