I figured that the DJANGO_SETTINGS_MODULE had to be set some way, so I looked at the documentation (link updated) and found:
export DJANGO_SETTINGS_MODULE=mysite.settings
Though that is not enough if you are running a server on heroku, you need to specify it there, too. Like this:
heroku config:set DJANGO_SETTINGS_MODULE=mysite.settings --account <your account name>
In my specific case I ran these two and everything worked out:
export DJANGO_SETTINGS_MODULE=nirla.settings
heroku config:set DJANGO_SETTINGS_MODULE=nirla.settings --account personal
Edit
I would also like to point out that you have to re-do this every time you close or restart your virtual environment. Instead, you should automate the process by going to venv/bin/activate and adding the line: set DJANGO_SETTINGS_MODULE=mysite.settings
to the bottom of the code. From now on every time you activate the virtual environment, you will be using that app's settings.
From The Definitive Guide to Django: Web Development Done Right:
If you’ve used Python before, you may be wondering why we’re running
python manage.py shell
instead of justpython
. Both commands will start the interactive interpreter, but themanage.py shell
command has one key difference: before starting the interpreter, it tells Django which settings file to use.
Use Case: Many parts of Django, including the template system, rely on your settings, and you won’t be able to use them unless the framework knows which settings to use.
If you’re curious, here’s how it works behind the scenes. Django looks for an environment variable called
DJANGO_SETTINGS_MODULE
, which should be set to the import path of your settings.py. For example,DJANGO_SETTINGS_MODULE
might be set to'mysite.settings'
, assuming mysite is on your Python path.When you run
python manage.py shell
, the command takes care of settingDJANGO_SETTINGS_MODULE
for you.**
Django needs your application-specific settings. Since it is already inside your manage.py
, just use that. The faster, but perhaps temporary, solution is:
python manage.py shell
In my case it was the use of the call_command
module that posed a problem.
I added set DJANGO_SETTINGS_MODULE=mysite.settings
but it didn't work.
I finally found it:
add these lines at the top of the script, and the order matters.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
import django
django.setup()
from django.core.management import call_command
In my case, it was a Python path issue.
django-admin dbshell
)(venv) shakeel@workstation:~/project_path$ export PYTHONPATH=/home/shakeel/project_path
(venv) shakeel@workstation:~/project_path$ export DJANGO_SETTINGS_MODULE=my_project.settings
(venv) shakeel@workstation:~/project_path$ django-admin dbshell
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite>
otherwise python manage.py shell
works like charm.
Create a .env file that will hold your credentials at the root of your project and leave it out of versioning:
$ echo ".env" >> .gitignore
In the .env file, add the variables (adapt them according to your installation):
$ echo "DJANGO_SETTINGS_MODULE=myproject.settings.production"> .env
#50 caracter random key
$ echo "SECRET_KEY='####'">> .env
To use them, put this on top of your production.py settings file:
import os
env = os.environ.copy()
SECRET_KEY = env['SECRET_KEY']
Publish it to Heroku using this gem: http://github.com/ddollar/heroku-config.git
$ heroku plugins:install git://github.com/ddollar/heroku-config.git
$ heroku config:push
This way you avoid to change virtualenv files.
*Based on this tutorial
If you are here due to error while trying to run daphne on server then here is the answer for you .
Change you asgi.py file like this.
import os
from django.conf.urls import url
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mc_backend.settings')
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
django_asgi_app = get_asgi_application()
from channels.auth import AuthMiddlewareStack
import api_backend.routing
application = ProtocolTypeRouter({
"http": django_asgi_app,
'websocket': AuthMiddlewareStack(
URLRouter(
api_backend.routing.websocket_urlpatterns
)
)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With