using the shell in Django - python

I am a newbie in Django un I want to write a Test for Django Web-poll application (https://docs.djangoproject.com/en/1.8/intro/tutorial01/)
but I got this error:
devuser#localhost:~/Django-apps/poolApp$ django-admin shell --plain --no-startup
Traceback (most recent call last):
File "/usr/local/bin/django-admin", line 9, in <module>
load_entry_point('Django==1.8.3', 'console_scripts', 'django-admin')()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/management/base.py", line 405, in run_from_argv
connections.close_all()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/utils.py", line 258, in close_all
for alias in self:
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/utils.py", line 252, in __iter__
return iter(self.databases)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/utils/functional.py", line 60, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/utils.py", line 151, in databases
self._databases = settings.DATABASES
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
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.
I would like to know what is the best approach to do it:
define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure()

Use manage.py, not django-admin.

What does work is using
django-admin shell --plain --no-startup --pythonpath "." --settings "myproject.settings"
while you are in the root of your django app.
However manage.py shell (or the amazing shell_plus from django_extensions https://github.com/django-extensions/django-extensions) is recommended instead

Related

Django 2.0 Tutorial - Cant Acess Django-Admin Shell

When I attempt to acess the Django Shell using Django-Admin Shell, I get the error message:
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.
(env2) Coreys-MacBook-Pro:mysite3 coreydickinson$
I've attempted the settings.configure() in the python shell, and I'ved tried redirecting the Django settings using django-admin.py shell --settings=mysite.settings. Neither worked. Anyone have any idea what I am doing wrong?
Full error:
(env2) Coreys-MacBook-Pro:mysite3 coreydickinson$ django-admin shell
Traceback (most recent call last):
File "/Users/coreydickinson/mysite/DJDev/env2/bin/django-admin", line 11, in <module>
sys.exit(execute_from_command_line())
File "/Users/coreydickinson/mysite/DJDev/env2/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/coreydickinson/mysite/DJDev/env2/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/coreydickinson/mysite/DJDev/env2/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/coreydickinson/mysite/DJDev/env2/lib/python3.6/site-packages/django/core/management/base.py", line 327, in execute
saved_locale = translation.get_language()
File "/Users/coreydickinson/mysite/DJDev/env2/lib/python3.6/site-packages/django/utils/translation/__init__.py", line 187, in get_language
return _trans.get_language()
File "/Users/coreydickinson/mysite/DJDev/env2/lib/python3.6/site-packages/django/utils/translation/__init__.py", line 55, in __getattr__
if settings.USE_I18N:
File "/Users/coreydickinson/mysite/DJDev/env2/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/Users/coreydickinson/mysite/DJDev/env2/lib/python3.6/site-packages/django/conf/__init__.py", line 41, in _setup
% (desc, ENVIRONMENT_VARIABLE))
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.
(env2) Coreys-MacBook-Pro:mysite3 coreydickinson$
You are supposed to use manage.py, not django-admin.py.

Fail with django app on azure

I'm trying to develop and deploy django app on azure web apps.
While following the tutorial on azure website, after cloning the basic project to my local machine (using git) and creating the virtual environment, I'm trying to run the createsuperuser commend, and getting this error:
C:\Users\tutzy\Desktop\newtest\amir1>env\scripts\python manage.py createsuperuser
Traceback (most recent call last):
File "manage.py", line 17, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\tutzy\Desktop\newtest\amir1\env\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "C:\Users\tutzy\Desktop\newtest\amir1\env\lib\site-packages\django\core\management\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\tutzy\Desktop\newtest\amir1\env\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\tutzy\Desktop\newtest\amir1\env\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 52, in execute
return super(Command, self).execute(*args, **options)
File "C:\Users\tutzy\Desktop\newtest\amir1\env\lib\site-packages\django\core\management\base.py", line 413, in execute
translation.activate(saved_locale)
File "C:\Users\tutzy\Desktop\newtest\amir1\env\lib\site-packages\django\utils\translation\__init__.py", line 154, in activate
return _trans.activate(language)
File "C:\Users\tutzy\Desktop\newtest\amir1\env\lib\site-packages\django\utils\translation\trans_real.py", line 216, in activate
_active.value = translation(language)
File "C:\Users\tutzy\Desktop\newtest\amir1\env\lib\site-packages\django\utils\translation\trans_real.py", line 205, in translation
_translations[language] = DjangoTranslation(language)
File "C:\Users\tutzy\Desktop\newtest\amir1\env\lib\site-packages\django\utils\translation\trans_real.py", line 118, in __init__
raise IOError("No translation files found for default language %s." % settings.LANGUAGE_CODE)
IOError: No translation files found for default language en-us.
It seems that your issue was similar with the other SO thread No translation file found for domain 'django' if you clone the Azure offical sample for Django like this. There is a *.mo line at the 49th line of .gitignore file. Please try to add the missing .mo file and do the createsupersuer operation again.
Hope it helps.

ImportError: cannot import name caches

I am using Django 1.6. I want to start working site. When you use python manage.py shell get an error:
(test) D:\test>python manage.py shell
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "D:\testvenv\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "D:\testvenv\lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\testvenv\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "D:\testvenv\lib\site-packages\django\core\management\base.py", line 280, in execute
translation.activate('en-us')
File "D:\testvenv\lib\site-packages\django\utils\translation\__init__.py", line 130, in activate
return _trans.activate(language)
File "D:\testvenv\lib\site-packages\django\utils\translation\trans_real.py", line 188, in activate
_active.value = translation(language)
File "D:\testvenv\lib\site-packages\django\utils\translation\trans_real.py", line 177, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "D:\testvenv\lib\site-packages\django\utils\translation\trans_real.py", line 159, in _fetch
app = import_module(appname)
File "D:\testvenv\lib\site-packages\django\utils\importlib.py", line 40, in import_module
__import__(name)
File "D:\testvenv\lib\site-packages\constance\backends\database\__init__.py", line 1, in <module>
from django.core.cache import caches
ImportError: cannot import name caches
Tell me what I have a problem. And if it can depend on your database (not sure that Django is connected to the DB - I do not know how to check)?
Added:
Code manag.py:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
By the looks of it you're using django-constance.
According to their change log, they haven't supported 1.6 since version 1.1
So either use an older version of that, or better yet - use a supported version of django.
According to the release notes, django.core.cache.caches was introduced in Django 1.7, so this application has never worked with Django 1.6.
If you have a running instance of that application, check which version of Django it actually uses.

`from django.conf import settings` is not working

I've changed my manage.py and settings.py and added a wsgi.py file to bring my Django 1.4 project closer to the modern age. This was done primarily because django_gunicorn is now deprecated. I'm using Python 2.7.9 and Django 1.4.19 in a PyCharm 4.0.6 development environment.
I also changed my directory structure from flat to nested.
/gse
manage.py
/gse
__init__.py
settings.py
urls.py
wsgi.py
...
/app1
/app2
manage.py
from os import environ
import sys
if __name__ == "__main__":
environ.setdefault("DJANGO_SETTINGS_MODULE", "gse.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gse.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
I installed django-avatar a while back, and now after the changes mentioned above it is breaking when I run manage.py validate.
I get the following traceback in PyCharm:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.6\helpers\pycharm\django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "C:\Python27\Lib\runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "C:\Python27\Lib\runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "C:\Python27\Lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Users\nu\gse\manage.py", line 23, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\__init__.py", line 443, in execute_from_command_line
utility.execute()
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\base.py", line 232, in execute
output = self.handle(*args, **options)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\base.py", line 371, in handle
return self.handle_noargs(**options)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\commands\validate.py", line 9, in handle_noargs
self.validate(display_num_errors=True)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\validation.py", line 30, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\db\models\loading.py", line 158, in get_app_errors
self._populate()
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\db\models\loading.py", line 64, in _populate
self.load_app(app_name, True)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\db\models\loading.py", line 88, in load_app
models = import_module('.models', app_name)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\utils\importlib.py", line 35, in import_module
__import__(name)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\avatar\models.py", line 23, in <module>
avatar_storage = get_storage_class(settings.AVATAR_STORAGE)()
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\files\storage.py", line 149, in __init__
self.location = abspathu(self.base_location)
File "C:\Users\nu\gseVirtualEnv\lib\ntpath.py", line 487, in abspath
path = _getfullpathname(path)
TypeError: coercing to Unicode: need string or buffer, tuple found
Process finished with exit code 1
I noticed that from django.conf import settings is used in django-avatars conf.py file. I tested the import statement in my project and discovered that PyCharm doesn't seem to be able to list the expected member variables from the settings class when I type settings.. My settings file correctly associated with Django in PyCharm.
Thank you Aamir.
When I looked at settings.py again I saw this:
MEDIA_ROOT = (os.path.join(os.path.dirname(__file__), '..', 'media').replace('\\','/'),)
AVATAR_STORAGE = MEDIA_ROOT + '\avatars'
Changing the MEDIA_ROOT assignment line to:
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), '..', 'media').replace('\\','/')
Fixed it.

How can I build Pinry (Python/Django Pinterest Clone) using PyCharm?

Before I get started please excuse the complete newbie question (request for dummy steps). I recently started learning Python and the Django framework using tools like Codecademy and Pluralsight and was hoping to dig into this by running Pinry (a Pinterest clone) using PyCharm, my IDE of choice for Python and work on modifying it with what I've learned so far to see what happens. Unfortunately, I download the latest version of Pinry and when I try to run it in PyCharm on my local machine I get all sorts of errors.
Here are the steps I take:
Python, Django, PyCharm are all installed and configured.
Download a new copy of Pinry and open the directory in PyCharm
Click on Run - Edit Configuration
Click on the + button to add a new configuration and check Run browse, select the Python interpreter (with Django) and click on Fix to enable Django support for the project
In the Project - Django settings, add the Django project root, settings and manage script
Finally hit alt/option + R and runserver to get the app started
At this point I would expect the package to run but instead I get the list of errors below.
Has anyone had any success in running Pinry in PyCharm without making any code modifications? I am just not sure if these are issues with the package or if its settings in PyCharm that need to be modified to make this work. Any insights?
THANK YOU!
File "/Applications/PyCharm.app/helpers/pycharm/django_manage.py", line 23, in <module>
run_module(manage_file, None, '__main__', True)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/Users/hussein/Desktop/pinry-master/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 279, in execute
saved_locale = translation.get_language()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 154, in get_language
return _trans.get_language()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 52, in __getattr__
if settings.USE_I18N:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/conf/__init__.py", line 49, in _setup
self._wrapped = Settings(settings_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/conf/__init__.py", line 151, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
In PyCharm "Project settings/Django" specify "settings/development.py" in "settings" field.

Categories