I've been learning Python for a few months and a couple days ago i decided to learn one of Web frameworks. I found some tutorials on Django and it was looking really cool until i tried to use "runserver" command. As you can see i used the basic line "python -m django runserver manage.py" and got a lot of calls. I've just installed Django and i wasn't changing anything in it's scripts. I only started by creating a new project that worked perfectly with "python -m django startproject djangoLearn". Do you have any thoughts on how can i fix this problem? This problem prevents me from further learning beacuse i can't see my own project :((
Thanks in advance. Hope y'all have a great day.
Calls that i get while trying to run server:
D:\Projects\djangoLearn>python -m django runserver manage.py
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\__main__.py", line 9, in <module>
management.execute_from_command_line()
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\commands\runserver.py", line 61, in execute
super().execute(*args, **options)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\commands\runserver.py", line 68, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\conf\__init__.py", line 82, in __getattr__
self._setup(name)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\conf\__init__.py", line 63, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I hope your virtualenv is activated ??
use this to create project django-admin startproject projectname . take note of the period it works without the period though.
use python manage.py startapp appname to create an app
don't forget to register the app in INSTALLED_APP
i think the code you want to use is python manage.py runserver
but if you use python3 use python3 manage.py runserver just use whatever you use to start your python before manage.py runserver
you should first activate your virtual environment. with a command like this in linux:
source "path-to-activate-file"
and command like this in windows:
\pathto\env\Scripts\activate
Related
I keep getting an Improperly Configured Error when I try to run my server
Error message:
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I created a new django project,
I have django installed in my virtual environment,
I did not forget to activate the virtual environment,
And I triple checked my python path,
Here is the full traceback:
(venv3) C:\Users\user\Desktop\trialproject>django-admin.py runserver
Traceback (most recent call last):
File "C:\Users\user\Desktop\trialproject\venv3\Scripts\django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\commands\runserver.py", line 60, in execute
super().execute(*args, **options)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\base.py", line 369, in execute
output = self.handle(*args, **options)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\commands\runserver.py", line 67, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
self._setup(name)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\conf\__init__.py", line 57, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Someone please help me figure out the problem. Thanks
that usually happen when you want to run your server with wrong virtual environment.
make sure you have the right virtual environment and try this :
python manage.py runserver
UPDATE : check if this is useful for you
(Django version 1.9, Python version 3.4.3, using virtual env made with mkvirtualenv --python=/usr/bin/python3.4 django19)
I've been following the Django tutorial very closely, without any problems up til part 5:
https://docs.djangoproject.com/en/1.9/intro/tutorial05/
although, strictly speaking, the problem isn't to do with part 5, but bear with me. I'm following the tutorial in the pythonanywhere framework, because eventually I would like to host a website there. Therefore, I am also following the 'Following the Django tutorial' offered by pythonanywhere:
https://help.pythonanywhere.com/pages/FollowingTheDjangoTutorial
although my problem isn't anticipated in the advice for part 5 in the tutorial. Now the problem- at the point in the Django tutorial which reads:
To check if the bug really exists, using the Admin create a question whose date lies in the future and check the method using the shell
Following the link to the shell, I try (as instructed in the link) to run:
django-admin shell --plain
However I get the following error message
(django19)19:11 ~/mysite $ django-admin shell --plain
Traceback (most recent call last):
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/base.py", line 391, in execute saved_locale = translation.get_language()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 176, in get_language
return _trans.get_language()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 57, in __getattr__
if settings.USE_I18N:
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 55, in __getattr__
self._setup(name)
File "/home/me/.virtualenvs/django19/lib/python3.4/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.
During handling of the above exception, another exception occurred:
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/base.py", line 360, in run_from_argv
connections.close_all()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/db/utils.py", line 230, in close_all
for alias in self:
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/db/utils.py", line 224, in __iter__
return iter(self.databases)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/db/utils.py", line 157, in databases
self._databases = settings.DATABASES
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 55, in __getattr__
self._setup(name)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 41, 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.
This error messages recommends setting DATABASES (which is set in mysite/settings.py), by defining the variable DJANGO_SETTINGS_MODULE. So, that's what I did:
export DJANGO_SETTINGS_MODULE="${PWD}/mysite/settings.py"
There are also the required __init__.py files in the mysite and mysite/mysite directories. However now when I run the django-admin command I get:
ImportError: No module named 'mysite/settings'
I can't find anymore documentation or help hereafter. What am I missing? Do I need to set something special somewhere else? Am I setting DJANGO_SETTINGS_MODULE correctly? The ImportError makes it sound like I should set DJANGO_SETTINGS_MODULE to mysite.settings or something similar, but this doesn't work either.
Once you have created your project, use manage.py instead of django-admin to run commands:
python manage.py shell
The manage.py command takes care of the settings for you. If you use django-admin, you have to set DJANGO_SETTINGS_MODULE manually. The DJANGO_SETTINGS_MODULE should be the Python module, e.g. mysite.settings, not a file like /path/to/mysite/settings.py.
Be sure to have your project in the python import search path. Try this
>>import sys
>>sys.path
if the path/directory for your project is not listed, then you need to do
>>sys.path.append('/path/to/my/django/project')
Then assign DJANGO_SETTINGS_MODULE like so:
$export DJANGO_SETTINGS_MODULE=mysite.settings
See Designating Django settings
I'm having problem with running Django manage.py runserver on my MAC OS X Yosemite.
When I ran it soon after restarting, everything works fine. But after several minutes, the running process will stop, and when I re-rerun manage.py runserver, it will generate this error
Performing system checks...
System check identified no issues (0 silenced).
January 22, 2015 - 15:10:11
Django version 1.7.3, using settings 'mycustomapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 83, in handle
self.run(*args, **options)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 92, in run
autoreload.main(self.inner_run, args, options)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/utils/autoreload.py", line 322, in main
reloader(wrapped_main_func, args, kwargs)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/utils/autoreload.py", line 288, in python_reloader
reloader_thread()
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/utils/autoreload.py", line 264, in reloader_thread
change = fn()
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/utils/autoreload.py", line 202, in code_changed
stat = os.stat(filename)
OSError: [Errno 2] No such file or directory: './manage.py'
Note that I am in the same directory as manage.py does. but for some reasons, it could not find it
here is what I found when I run ls:
manage.py requirements.txt mycustomapp
My django version is 1.7.3, OS YOSEMITE, I had all brew updated and upgraded
Any help would be appreciated
Thanks !
There's no need to upgrade or degrade your Django version for an OSError [Error123].
I got this error for weeks and I thought about the actual reason behind this error because my code and files were all perfect! But then, after weeks of observation of my code, I figured out the hell of that mistake!
ANSWER - Please create your APPS, STATIC-FOLDER, TEMPLATE-FOLDER, and further things within your PROJECT!!
DEMONSTRATION -> { '>>' indicates next step}
OPEN POWERSHELL >> mkdir DIRECTORY_NAME >> cd DIRECTORY_NAME >> code .
this will open visual studio code within the directory named (DIRECTORY_NAME)
OPEN TERMINAL >> install django >> django startproject PROJECT_NAME >>
cd PROJECT_NAME >> python manage.py runserver >> COME OUT OF THE SERVER >> django startapp APP_NAME >> CONTINUE YOUR WORK IN THE VS CODE AND ONLY COM TO TERMINAL TO RUN SERVER.
I've managed to resolve the issue. Turns out since in this Django version I would need to separate the wsgi file for development and production use. therefore wsgi_prod.py and wsgi.py . This action was not needed in previous versions of Django.
I decided to try developing on python with django, while python is new to me right now.
[I'm using Windows 7 64bit]
Then, i installed python 2.7, added C:\Python27\;C:\Python27\Scripts; to PATH variable.
Next, i installed Eclipse Classic (last version) and installed PyDev, and Django.
I created my first project on Eclipse as a Django project but couldn't run it.
I'm trying (Right click on my project)-> Run as -> PyDev: Django, and i have the following console log:
Validating models...
Traceback (most recent call last): File "C:\Users\Matan\workspace\FirstSample\manage.py", line 10, in <module>
execute_from_command_line(sys.argv) File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line 443, in execute_from_command_line
utility.execute() File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__) File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 232, in execute
output = self.handle(*args, **options) File "C:\Python27\Lib\site-packages\django\core\management\commands\runserver.py", line 69, in handle
self.run(*args, **options) File "C:\Python27\Lib\site-packages\django\core\management\commands\runserver.py", line 80, in run
and the list goes on...
So i tried to run the project with the python console, using:
import Django
manage.py runserver
and i have the following syntax error:
File "<stdin>", line 1
manage.py runserver
^ SyntaxError: invalid syntax
What is the problem with running my project ? How can i solve this?
Thanks.
Edit:
After trying to run the project with the following command:
python manage.py runserver on the project directory, i receive the following traceback log: Validating models...
Unhandled exception in thread started by <bound method Command.inner_run of <dja ngo.contrib.staticfiles.management.commands.runserver.Command object at 0x000000 0002688748>> Traceback (most recent call last): File "C:\Python27\lib\site-packages\django\core\management\commands\runserver. py", line 91, in inner_run
self.validate(display_num_errors=True) File "C:\Python27\lib\site-packages\django\core\management\base.py", line 266, in validate
num_errors = get_validation_errors(s, app) File "C:\Python27\lib\site-packages\django\core\management\validation.py", lin e 23, in get_validation_errors
from django.db import models, connection File "C:\Python27\lib\site-packages\django\db\__init__.py", line 40, in <modul e>
backend = load_backend(connection.settings_dict['ENGINE']) File "C:\Python27\lib\site-packages\django\db\__init__.py", line 34, in
__geta ttr__
return getattr(connections[DEFAULT_DB_ALIAS], item) File "C:\Python27\lib\site-packages\django\db\utils.py", line 92, in
__getitem
__
backend = load_backend(db['ENGINE']) File "C:\Python27\lib\site-packages\django\db\utils.py", line 24, in load_back end
return import_module('.base', backend_name) File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in im port_module
__import__(name) File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 16 , in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No mo dule named MySQLdb
Download the pre-compiled python-mysql package for Windows, install it, then run the command again.
While you are there, you might as well also install setuptools if you haven't already.
Error loading MySQLdb module: No mo dule named MySQLdb
You have configured your settings to you MySQL but you have not installed MySQL-python:
pip install MySQL-python
You must be in the project directory to run the python manage.py runserver command. You also need to (1) set the PYTHONPATH environment variable to include your project directory's parent and (2) set the DJANGO_SETTINGS_MODULE environment variable to yourproject.settings
If you've done it correctly, you should be able to go to an unrelated directory (i.e. not your project directory) and do the following import in a python shell:
c:\temp> python
...
>>> from django.conf import settings
the settings module that is imported should then be the one in your project directory...
I am trying to learn django by following along with this tutorial. I am using django version 1.1.1
I run
django-admin.py startproject mysite
and it creates the files it should. Then I try to start the server by running
python manage.py runserver
but here is where I get the following error.
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 362, in execute_manager
utility.execute()
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 303, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 195, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 213, in execute
translation.activate('en-us')
File "/Library/Python/2.6/site-packages/django/utils/translation/__init__.py", line 73, in activate
return real_activate(language)
File "/Library/Python/2.6/site-packages/django/utils/translation/__init__.py", line 43, in delayed_loader
return g['real_%s' % caller](*args, **kwargs)
File "/Library/Python/2.6/site-packages/django/utils/translation/trans_real.py", line 205, in activate
_active[currentThread()] = translation(language)
File "/Library/Python/2.6/site-packages/django/utils/translation/trans_real.py", line 194, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "/Library/Python/2.6/site-packages/django/utils/translation/trans_real.py", line 172, in _fetch
for localepath in settings.LOCALE_PATHS:
File "/Library/Python/2.6/site-packages/django/utils/functional.py", line 273, in __getattr__
return getattr(self._wrapped, name)
AttributeError: 'Settings' object has no attribute 'LOCALE_PATHS'
Now, I can add a LOCALE_PATH atribute and set to an empty tuple to my settings.py file but then it just complains about another setting and so on. What am I missing here?
Something is broken in your django installation. maybe you have a (very) old version somewhere in the path?
LOCALE_PATHS was given a default value in the global settings file a long time ago.
Can't really explain that. Try removing the project directory and starting again.
Are you definitely running the manage.py from within the directory with the settings file?