Unable to config django for mysql - python

I'm trying to connect Django app to MySQL on windows 10. I have installed mysqlclient as an interface between my Django app and MySQL. Whenever I try to open dbshell I'm getting error something like this:-
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured
You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Here is my setting.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'blog',
'USER':'root',
'PASSWORD':'root',
'HOST':'127.0.0.1',
'PORT':'3306'
}
}
I have tried other solutions on StackOverflow but it didn't work for me.
I also tried deleting the dbsqlite3 file from the project directory.
Versions for different elements are as follows:-
Python (3.6)
Django (1.11.4)
mysqlclient (1.3.12)
pip (9.0.1)
MySQL(5.7)

DJANGO_SETTINGS_MODULE variable is not available. run
python manage.py shell
if you linux user and run
python3 manage.py shell
as you use python 3.6

Finally, I solved this problem. I was using django-admin dbshell command instead of python manage.py dbshell
In case of windows, set env variable for MySQL in user variables section, otherwise you will get CommandError saying that mysql is not installed.
For Ubuntu use python3 manage.py dbshell

Related

Wagtail 2.14.2 ModulNotFound error after running makemigrations

3rd try of wagtail
I did install it after having created a virtual environment named wag-env with virtualenv within the directory websites/wag/, not in the .virtualenv folder. Command was "virtualenv wag-env".
Did pip install wagtail
Then ran wagtail start ready_wag
I then ran python3 manage.py makemigrations and get this error messages "ModuleNotFound error wagtail.models". A check in the lib confirmed that there is a models folder at wagtail root level, but no models.py.
That's my third try since yesterday, first was with mkvirtualenv, had the same problem, second was with virtualenv, trying to integrate a finished django project, and now trying to start a project from scratch.
I ran a pip freeze --user, there's a wagtail installed globally. Should I get rid of it ?
Thanks in advance for your answers
I've always used venv to create my environments, never had issue. Sounds like you have conflicting versions of Django to match your version of Wagtail.
Here are my old notes from learning Wagtail, the process I still generally use for every project:
Create project folder, open terminal session in that folder
In project folder, create virtual environment (python -m venv .venv)
Start virtual environment (.\.venv\Scripts\Activate.bat or source .venv/bin/activate)
Install Wagtail (pip install wagtail==3.0.3) <- adjust version as needed
Start new project in current directory (wagtail start project .)
Install wagtail requirements (pip install -r requirements.txt)
If using POSTGRES, follow the POSTGRES setup instructions BEFORE migrating.
Provision database (python manage.py migrate)
Run server and check default site (python manage.py runserver)
Create superuser (python manage.py createsuperuser) and log in to admin area
Move database settings, allowed hosts and secret key from base.py to new file local.py in the same folder. Also any other settings that apply only to the local machine.
Create the git repository.
Setting up POSTGRES backend
With POSTGRES installed, from the command prompt, enter the POSTGRES command prompt:
psql -U username
username is the db global admin login created during install
Enter the following commands to add a project database and a user with full rights on the project db:
CREATE DATABASE myproject;
CREATE USER myprojectuser WITH PASSWORD 'password';
ALTER ROLE myprojectuser SET client_encoding TO 'utf8';
ALTER ROLE myprojectuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myprojectuser SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;
Type \q to exit the POSTGRES prompt.
In your virtual environment, install psycopg2:
pip install psycopg2
Note, you may get a warning to install from an alternative source:
pip install psycopg2-binary
Create a blank database for the project. Set this up in the project local.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'projectdb',
'USER': 'myprojectuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
}
}
Continue on to step 8 above.

Django py manage.py makemigrate

What can I make with it? I'm beginner in python and django. I download it and I i wrote py manage.py makemigrate and I've get error. Can u help me?
Your issue is with your DB configuration in the setting.py. If you are using the default SQLite then copy/paste this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
and your project will work just fine. After this, run
python manage.py makemigrations
python manage.py migrate #copy all migrations to the database
python manage.py createsuperuser #to have a admin user to login to adminpanel
python manage.py runserver #starting the server
Otherwise, take a look at the official documentation how to connect MySQL, PostgreSQL, Oracle databases and required configurations.
Your error is in here:
SQLite is not like MySQL or other databases. Actually, it is not a real database. You are using a port, username, password and etc. These are the cause of the error. SQLite is not running in the server or another place. It is just a single file contains data information. Update yours to mine above and it should start work again or change your database to MySQL or others.
You need to supply all environment variables that are listed in your settings file. Such as DB_NAME that presented in your screenshot. Search for os.environ[<VARIABLE_NAME>], every VARIABLE_NAME should be defined.
If you are a beginner it is better to stay with the documentation and do like https://docs.djangoproject.com/en/2.1/intro/tutorial01/
If you could share the DB part of the settings.py it would help.
Generally python manage.py startapp appname should create the necessary files for you.
After which a python manage.py makemigrations and python manage.py migrate should work properly. And this should not come.

ImproperlyConfigured: Requested settings DATABASES

I have seen 10 different ways of configuring the MySQL database with Django, and none of them work.
I am running MySQL 8.0 and
Django 2.0
I updated the Project/settings.py with the database settings as followed:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'name_of_db',
'USER': 'root_user',
'PASSWORD': 'not_telling_u_my_pass_:)',
'HOST': 'localhost',
'PORT': '3306',
}
}
When I run the command to test the connectivity:
python manage.py dbshell
I get error:
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
No other settings have been made, however, I have tried to follow a few guides which say that I must declare:
set DJANGO_SETTINGS_MODULE=mysite.settings
I have also issued:
python manage.py shell
With no luck. I am running Windows 7 as well. I have all of the mysql dependencies installed for connecting from Python to MySQL. I know this question has been asked a number of times, but a thread should exist with clear instructions. Many of the other threads do not have clear and concise instructions, and I'm using a later version of MySQL so hopefully this will help solve others issues experiencing the same.
Project Tree:
Server
|
+-- Server
| |
| +-- _init_.py
| +-- settings.py
| +-- urls.py
| +-- wsgi.py
+-- manage.py
Update 1 (10 mins after posting):
I ran the python manage.py migrate and this installed all of the tables into my database schema. However, when I run the python manage.py dbshell command again, I still get the same error above saying the settings are not configured.
Update 2:
After running:
python manage.py shell --settings=Server.settings
I get:
python manage.py dbshell
CommandError: You appear not to have the 'mysql' program installed or on your path.
The ImproperlyConfigured often masks other errors. As you note, your database configuration is correct, which means the problem is most likely one of two problems.
Some file imported during setup is causing an exception
There is an import in settings.py which is in turn trying to use a setting (i.e., settings is used while being loaded)
First, if you have any imports in your settings.py, make sure those aren't going to in turn import django.conf.settings (imports like import os) are fine.
Next, inspect files from your project that might be imported during setup (apps, urls, custom middleware, template contexts, etc.) for possible errors that might arise during import. Make sure you can import all files on their own (some may complain because they require setup to be run first, you'll just have to inspect those extra carefully, or remove references to settings so that you can test them on their own).
I met the same problem and I have fixed the problem finally. The reason is that MySql will treat different when you using the 'localhost' parameter. It will connect to the local server using a Unix socket file.
Check this document:
https://dev.mysql.com/doc/refman/5.7/en/connecting.html
So you should use '127.0.0.1' instead of 'localhost' in DATABASES.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'name_of_db',
'USER': 'root_user',
'PASSWORD': 'not_telling_u_my_pass_:)',
'HOST': '127.0.0.1', #This string will works!
'PORT': '3306',
#If you want to use 'localhost', you should provide the path
# of the UNIX socket. You should check /etc/my.cnf(the mysql
# config file) to get this parameter.
'OPTIONS': {'unix_socket': '/tmp/mysql.sock'}
}
}
That will work in MySql 5.7, I am not test in MySql 8.0. Another suggestion: check user privileges in MySql, may be something wrong.

ElasticBeanstalk Django: Please supply ENGINE value

I am trying to install a Django app with ElasticBeanstalk. It's failing with the exception settings.DATABASES is improperly configured. Please supply the ENGINE value
Full error:
Command failed on instance. Return code: 1 Output: (TRUNCATED)... in
complain raise ImproperlyConfigured("settings.DATABASES is improperly
configured. " django.core.exceptions.ImproperlyConfigured:
settings.DATABASES is improperly configured. Please supply the ENGINE value.
Check settings documentation for more details.
container_command 01_migrate in .ebextensions/15_my.config failed. For more
detail, check /var/log/eb-activity.log using console or EB CLI.`
I am getting this error while one of my command is getting executed:
01_migrate:
command: "source /opt/python/run/venv/bin/activate && python manage.py migrate --noinput"
leader_only: True
Here's my config/settings/production.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'ebdb',
'USER': 'djangousername',
'PASSWORD': 'djangopassword',
'HOST': env('RDS_HOSTNAME'),
'PORT': '5432'
}
}
I am kind of sure that EB is picking the config from this file only and not any other file because if I am changing something in this file, it is reflected while ElasticBeanstalk env creation. I have also searched in my whole application that DATABASES is mentioned only in either this file i.e. <app-rot>/config/settings/production.py or <app-root>/config/settings/local.py. At both the places, I have provided the ENGINE value as above.
Please let me know if I need to add any other relevant information
This took me forever to figure out (as most things with Django and EB). I think the environment variables you set in your console take preference over variables or settings you set anywhere else e.g. .ebextensions.
In my case the error was because my settings file was set correctly in my ebextensions but incorrectly in the console variables. Thanks #daniel for the hint

Configuring Postgres on Heroku with Django

I am trying to link a Heroku Postgres databse with my Django application. I have created the database and linked it to my Heroku application using this tutorial. I can't seem to get it working with Django, however.
I am able to access the database through the heroku pg:psql command. But when I try to run python manage.py migrate, Django gives...
settings.DATABASES is improperly configured
Please supply the ENGINE value.
In settings.py, I have...
import dj_database_url
DATABASES = {
'default': dj_database_url.config(default=os.getenv('DATABASE_URL'))
}
I have already promoted the correct heroku postgres database to my app's DATABASE_URL variable, which I can verify through heroku config.
You've misread the instructions. It should be:
DATABASES = {
'default': dj_database_url.config()
}
dj_database_url already parses the env var, you don't need to pass it explicitly.
Heroku config variables aren't available in Django settings.py when testing locally.
https://stackoverflow.com/a/21763381/3783608

Categories

Resources