136
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.
113
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]-How do I do an OR filter in a Django query?
- [Django]-How can I upgrade specific packages using pip and a requirements file?
- [Django]-Django optional URL parameters
74
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
- [Django]-Django models avoid duplicates
- [Django]-Django TemplateDoesNotExist?
- [Django]-Execute code when Django starts ONCE only?
55
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
- [Django]-405 "Method POST is not allowed" in Django REST framework
- [Django]-How to add superuser in Django from fixture
- [Django]-Django: guidelines for speeding up template rendering performance
12
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
)
)
})
- [Django]-What is the Simplest Possible Payment Gateway to Implement? (using Django)
- [Django]-Django β No module named _sqlite3
- [Django]-Creating a JSON response using Django and Python
8
In my case, it was a Python path issue.
- First set
your PYTHONPATH
- then set
DJANGO_SETTINGS_MODULE
- then run
django-admin
shell command (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.
- [Django]-Why am I getting this error in Django?
- [Django]-Django form: what is the best way to modify posted data before validating?
- [Django]-Django Cache cache.set Not storing data
6
Iβve got this error while trying to import models from python console. My workaround:
import os, django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_app.settings")
django.setup()
my_app.settings
is path to yoursettings.py
And then you can import your models:
from users.models import UserModel
- [Django]-Fastest way to get the first object from a queryset in django?
- [Django]-Django Rest Framework β Updating a foreign key
- [Django]-Complete django DB reset
4
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
- [Django]-How to add a cancel button to DeleteView in django
- [Django]-How to implement followers/following in Django
- [Django]-Django. A good tutorial for Class Based Views
3
I have used pipenv to manage virtual environment for django project.
SOLUTION
1. pipenv shell
2. python manage.py shell
- [Django]-Where to put business logic in django
- [Django]-How can I disable logging while running unit tests in Python Django?
- [Django]-How to test auto_now_add in django
2
If you are using the local server, run Django shell using python manage.py shell
. It will take you to the Django python environment and you are good to go.
- [Django]-Django: Why do some model fields clash with each other?
- [Django]-How to delete project in django
- [Django]-How can I build multiple submit buttons django form?
2
I had a wrong way of import.
First I imported models of django without (before I declare that the file use django)
import pika, json, os, django
from products.models import Product
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "admin.settings")
django.setup()
But then I changed the import as below and it works (basically declaring file uses django and then importing models and other apps)
import pika, json, os, django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "admin.settings")
django.setup()
from products.models import Product
- [Django]-Is it secure to store passwords as environment variables (rather than as plain text) in config files?
- [Django]-Django model CharField: max_length does not work?
- [Django]-Django Sitemaps and "normal" views
0
I found this error when I set in admin.py my list_display = ('name',)
I changed it to list_display = ('name','date')
And it worked.
- [Django]-Get object by field other than primary key
- [Django]-Error: No module named staticfiles
- [Django]-How can I keep test data after Django tests complete?
0
For me I just had to introduce the missing variables through "Preferences" => "Terminal" => "Environment variables"
:
- Go to
Preferences (cmd +,)
- Go to
Terminal
and add two Environment
variables (PYTHONPATH and DJANGO_SETTINGS_MODULE)
PYTHONPATH
pointed to the absolute path of the project root.
DJANGO_SETTINGS_MODULE
pointed to the settings file name (relative to the root path and without .py
)
See the attached picture, please!
- [Django]-Get user profile in django
- [Django]-Passing variable urlname to url tag in django template
- [Django]-Specifying limit and offset in Django QuerySet wont work
0
Edit the Settings.py file probably located at projects\projects folder, and Add your Apps name like below example:
INSTALLED_APPS = [
βdjango.contrib.adminβ,
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'YourAppsName'
- [Django]-Django queryset filter β Q() | VS __in
- [Django]-Django import error β No module named core.management
- [Django]-How to access array elements in a Django template?