Rerunning the Django Project - mysql

Intitally when i setup i didn't have any error when i typed python manage.py runserver. However when i installed mysql and changed admins and databases in my settings.py, i can't seem to run the server again.
Setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
When i run python manage.py run server:
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

YOu have to make sure to active your env as you noticed. As you can see the django package ONLY exists in your env. If you are not using it then you can not access any part of the django package (django.core.management) THere are many man tutorials explaining how virtualenv functions.
http://www.arthurkoziel.com/2008/10/22/working-virtualenv/
http://iamzed.com/2009/05/07/a-primer-on-virtualenv/

Related

An unhandled lowlevel error occurred. Missing `secret_key _base` for 'production' environment

My ruby ​​version is "2.6.6", my mysql version is "5.7", my mysql2 version is 0.5.3, my rails version is 5.0.7.2, and my Xcode version is 12.5.
I use macOS Big Sur(version 11.4) and the text editor "Atom".
I'm planning to release my Rails application with the URL of HEROKU(example. https://[My APP Name].herokuapp.com).
I caused the error "Missing secret_key _base for 'production' environment" even though I set up the environment variable.
database.yml
# MySQL. Versions 5.0 and up are supported.
#
# Install the MySQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.7/en/old-client.html
#
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: <%= ENV.fetch("DATABASE_USERNAME") %>
password: <%= ENV.fetch("DATABASE_PASSWORD") %>
socket: /tmp/mysql.sock
development:
<<: *default
database: ****_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: ****_test
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="mysql2://myuser:mypass#localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:
adapter: postgresql
encoding: unicode
pool: 5
database: ****_production
username: ****
password: <%= ENV['****_DATABASE_PASSWORD'] %>
secrets.yml
# Be sure to restart your server when you modify this file.
# Your secret key is used for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
# You can use `rails secret` to generate a secure secret key.
# Make sure the secrets in this file are kept private
# if you're sharing your code publicly.
development:
secret_key_base: ****
test:
secret_key_base: ****
# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
I added the GENERETED CODE of rails secret(I also executed bundle exec rake secret.) by the four following methods.
1. $ export SECRET_KEY_BASE=GENERATED CODE
2. $ heroku config:set SECRET_KEY_BASE=GENERATED CODE
I added the GENERATED CODE to ~/.bash_profile.
export SECRET_KEY_BASE=GENERATED CODE
And then I saved the above SECRET_KEY_BASE with esc, ":x" and Enter.And I executed this command$ source ~/.bash_profile.
I added the GENERATED CODE to ./env file
DATABASE_USERNAME = ****
DATABASE_PASSWORD = ****
SECRET_KEY_BASE = GENERATED CODE
end.
I verified that the environment variable is set in Linux by the three following methods:
$ heroku config:get SECRET_KEY_BASE
GENERATED CODE
or
$ printenv | grep SECRET_KEY_BASE
SECRET_KEY_BASE=GENERATED CODE
and
$ echo $SECRET_KEY_BASE
GENERATED CODE
end.
As a result, Heroku didn't open but there are two messages of the error:
I executed this command:$ heroku open.
But Heroku didn't open but the following message was displayed.
An unhandled lowlevel error occurred. The application logs may have details.
I executed this command:$ heroku logs and the following message was displayed.
#<RuntimeError: Missing `secret_key
_base` for 'production' environment, set this value in `config/secrets.yml`>
end.
I can't open Heroku ,even though I set up the environment variable ,according to the two above messages.
Maybe I think that I can't open Heroku because MySQL version currently in use "5.7" and MySQL version with this command $ mysql --version don't match.
I executed this command$ mysql --version.
$ mysql --version
mysql Ver 8.0.23 for osx10.16 on x86_64 (Homebrew)
end.
The following is the evidence that I use MySQL"5.7".
$ brew services start mysql#5.7
==> Successfully started `mysql#5.7` (label: homebrew.mxcl.mysql#5.7)
end.
What's the true cause why I can't open Heroku?
First, a couple of details:
MySQL has nothing to do with this error, so you can edit that out from your question.
Nothing in your local linux machine environment has nothing to do with the error, so you can remove any reference to printenv, echo and export.
My guess: config/secrets.yml is git-ignored
The most likely reason heroku does not see your SECRET_KEY_BASE env variable is that the secrets.yml file itself never goes to heroku.
If the config/secrets.yml file is never uploaded from your computer, heroku has no way to find it. Unlike DATABASE_URL, the secret key base does not have a "default" environment variable from which it is read.
How to check
If your project is in github/gitlab/other, you can check there, if you cannot find config/secrets.yml then it's not going to get to heroku either. If instead you don't have any other remote, check your .gitignore file (it contains the list of files that should not be uploaded when you push, to say it in some way); it probably includes a line saying config/secrets.yml.
Another way to check this is to change something (even a comment) in config/secrets.yml, and save. Then write git status in the same directory: if it says "no changes", then you have your config/secrets.yml ignored.
What to do
The easiest way is to go to config/environments/production.rb, and set your secret key base directly:
# somehwere inside the configure block
config.secret_key_base = ENV["SECRET_KEY_BASE"]
Once you commit and push this change, you should be all set.

MySQL (MariaDB) backend for Django under Python3 on Debian

I am attempting to configure Django running under Python3 on a Debian box to use MariaDB as its backend. If I alter my mysite/settpings.py as per the tutorial i.e.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'foo',
'USER': 'tim',
'PASSWORD': 'swordfish',
'HOST': 'localhost'
}
}
I get a lot of grief, culminating in
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py", line 30, in <module>
'Did you install mysqlclient or MySQL-python?' % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'.
Did you install mysqlclient or MySQL-python?
Now, obviously, MySQLdb doesn't work on Python3 and although I have installed the mysql connector package doesn't seem to work.
If I try using the connector recommended by MySQL
i.e.
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'foo',
'USER': 'tim',
'PASSWORD': 'swordfish',
'HOST': 'localhost'
then I get a load of grief culminating in
django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
Error was: cannot import name 'BaseDatabaseFeatures'
Any suggestions?
UPDATE
Further to Matt Seymour's suggestion if I attempt a pip (actually pip3 as my system Python is still 2.x) install of mysqlclient I get the following...
tim#merlin:~/mysite$ sudo pip3 install mysqlclient
Collecting mysqlclient
Using cached mysqlclient-1.3.10.tar.gz
Complete output from command python setup.py egg_info:
/bin/sh: 1: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-9uub69zo/mysqlclient/setup.py", line 17, in <module>
metadata, options = get_config()
File "/tmp/pip-build-9uub69zo/mysqlclient/setup_posix.py", line 44, in get_config
libs = mysql_config("libs_r")
File "/tmp/pip-build-9uub69zo/mysqlclient/setup_posix.py", line 26, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
OSError: mysql_config not found
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-9uub69zo/mysqlclient
Could the fact that I am using MariaDB rather than MySQL be significant here?
OK, after some messing around I have an answer of sorts, although not as satisfactory as I would like.
The MySQLdb library isn't supported under Python 3.x, and so you should use the mysqlclient fork of it instead. However, because I am using the MariaDB fork of MySQL, some of Debian's dependencies required to install mysqlclient are unmet. I don't want to revert to MySQL as I am using MariaDB for other purposes.
So, as all I needed was a fairly robust, multi-user back-end RDBMS, I've installed PostgreSQL for use with Django which seems to work nicely enough.

how to configure django with mysql using pymysql (python 3.5)

having much trouble getting mysql(5.6) to connect with django (1.11) using python 3.5.2 in ubuntu(16.04) venv. im trying to use pymysql because it works great with flask applications, just not django yet. mysqlclient failed install and has other dependencies (ive read django docs yes) so would prefer avoiding that module. this really should work since it works flawless in flask and others seem to have it working. heres what Ive tried and found:
# first steps
pip install pymysql
mysql -u root -p
CREATE DATABASE djang0 CHARACTER SET utf8 COLLATE utf8_bin;
# manage.py
try:
import pymysql
pymysql.install_as_MySQLdb()
except ImportError:
pass
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djang0',
'USER': 'root',
'PASSWORD': 'mypasswd',
'HOST': '',
'PORT': '3306',
}
}
# current models.py Post class for blog
class Post(models.Model):
title = models.CharField(max_length=140)
body = models.TextField()
date = models.DateTimeField()
image = models.ImageField(upload_to='uploads', blank=True)
# error - full here https://dpaste.de/vtEH
[Thu Apr 27 18:58:00.010964 2017] [wsgi:error] [pid 22811:tid 3049028416] [remote 192.168.0.3:52267] django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'.
[Thu Apr 27 18:58:00.010967 2017] [wsgi:error] [pid 22811:tid 3049028416] [remote 192.168.0.3:52267] Did you install mysqlclient or MySQL-python?
had to use dependency, pip install mysqlclient. boo

Django mysql connectio No module named mysql.base

I try to connect MySQL from django application serverd in AWS but it raises an interesting error: Error was: No module named mysql.base
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'try',
'USER': 'root',
'PASSWORD': 'pwd',
'HOST': '',
'PORT': '3306',
}
}
MySQLdb is already installed. import MySQLdb runs in python command line.
I can connect MySQL. I have installed it by sudo apt-get install mysql-server mysql-common mysql-client libmysqlclient15-dev
I am really stuck with this error. Could you help to figure this out?
Thanks
It seems to me that you haven't installed mysql for python.
http://pypi.python.org/pypi/MySQL-python/1.2.3
or
sudo apt-get install python-mysqldb
Seems like a problem with your Python PATH. Make sure the python-mysql db driver is in the PATH.

Problem running syncdb in django

I am trying to go through the django docs tutorial and having a problem syncing mysql. On the command python manage.py syncdb I get the following error (note I'm running in windows 7):
...
File "C:\python27\lib\site-packages\django\db\backends\mysql\base.py", line 14, in <module>
raise Improperlyconfigured("Error loading Mysqldb module: %s" % e)
django.core.excepions.Improperlyconfigured: Error loading Mysqldb module: No module named mySQLdb
I have initialized the db in setting.py as:
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysite', #the name I gave in Mysql with 'CREATE DATABASE mysite;'
'USER': 'root',
'PASSWORD': 'mypassword', # as set in MysqlInstanceConfig
'HOST': '',
'PORT': '',
So how do I get syncdb to run correctly? What does the missing module error mean and how do I correct it?
You need to install the python mysql library.
Here is a django-related guide to do this on windows
You have to install the mySQLdb module.
You most probably haven't installed the Python module that handles the mysql connections. You can install it through the Cheeseshop with pip or easy_install and it's called MySQL-python.