18
Call to django.setup()
should go after setting DJANGO_SETTINGS_MODULE
environment variable. Just move it into your __main__
right after os.environ.setdefault()
.
35
You can insert os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
before the django.setup()
line.
- [Django]-Best way to make Django's login_required the default
- [Django]-How to move a model between two Django apps (Django 1.7)
- [Django]-DateTimeField doesn't show in admin system
14
If you are getting a similar error after initiating your interaction with Django by running python
in a terminal, running python manage.py shell
in the appropriate directory may solve your issue.
- [Django]-Django dynamic model fields
- [Django]-Django-debug-toolbar not showing up
- [Django]-Django: Calculate the Sum of the column values through query
2
For development and debugging, you may use the standalone python package.
Install with pip
pip install standalone
Use standalone to use Django from the module.
# add the following to the top of the module.
import standalone
standalone.run('mysite.settings') # replace with your settings module.
# your code goes below the configuration
import os
import sys
# ... .. . whole module
Now you may run the module as a Python Django script.
- [Django]-Automatic creation date for Django model form objects
- [Django]-Django unit testing with date/time-based objects
- [Django]-Django redirect to view
1
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'epl.settings')
should be above
import django
which is you should call django_settings_module before importing and calling django setup.
Hope this is helpful.
- [Django]-Detect mobile, tablet or Desktop on Django
- [Django]-Memory efficient (constant) and speed optimized iteration over a large table in Django
- [Django]-Adding to the "constructor" of a django model
1
If you’re running PyCharm, what worked for me was to invalidate the cache and restart the app.
File => Invalidate Caches / Restart ...
My virtualenv had been recently updated from Python 3.6 or 3.7 to 3.8.
- [Django]-How to define a default value for a custom Django setting
- [Django]-TypeError: login() takes 1 positional argument but 2 were given
- [Django]-ChoiceField doesn't display an empty label when using a tuple
0
It happened to me when I used django related import statement in a non-django module.
i.e from index.views import view
that did raise an error for me.
- [Django]-Django ManyToMany model validation
- [Django]-Logging in Django and gunicorn
- [Django]-Order of Serializer Validation in Django REST Framework
0
I met the same problem when setting the environment.
(I tried to add the:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
into the “Starting script window”,which is in the Django Console or rewrite the .wsgi file, But all of them were failed)
My final solution: (open the terminal and set the environment manually)
Here are the steps:(for mac)
-
open the terminal and type vim ~/.bash_profile
-
add below lines:
PATH=${PATH}:/Users/work/workspace/[virtual_environment]/bin PYTHONPATH=${PATH}:/Users/work/PycharmProjects/[project_name] DJANGO_SETTINGS_MODULE=[project_name].settings
-
type :wq to save and exit terminal
-
type source ~/.bash_profile to reload it
it will work when you run python manage.py
shell this time.
- [Django]-Django ModelChoiceField: filtering query set and setting default value as an object
- [Django]-Django 0.0.0.0:80; can't access remotely
- [Django]-Aggregate (and other annotated) fields in Django Rest Framework serializers
-1
Make sure to activate your venv first by scripts/activate venv then in your populate_user.py right after import os run os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘epl.settings’) this should work.
- [Django]-How do I get the current date and current time only respectively in Django?
- [Django]-Where should utility functions live in Django?
- [Django]-Django create userprofile if does not exist