Django errors
Python cannot find Django
(docker) eth1.ru@server:~/HelloDjango [0] $ python3 manage.py runserver
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 14, in <module>
) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
The reason is that the path to python is not in PATH. Go back to the step PATH
Disallowed host
You can start Django with
python3 manage.py runserver
And you added to settings.py your hosts, for example like this
ALLOWED_HOSTS = ['http://www.eth1.ru','127.0.0.1','localhost','eth1.ru','www.eth1.ru']
But when you open the domain in the browser the DisallowedHost error appears
DisallowedHost at / Invalid HTTP_HOST header: 'www.eth1.ru'. You may need to add 'www.eth1.ru' to ALLOWED_HOSTS. Request Method: GET Request URL: http://www.eth1.ru/ Django Version: 2.1.5 Exception Type: DisallowedHost Exception Value: Invalid HTTP_HOST header: 'www.eth1.ru'. You may need to add 'www.eth1.ru' to ALLOWED_HOSTS. Exception Location: /home/a/andreyolegovichru/.local/lib/python3.7/site-packages/django/http/request.py in get_host, line 106 Python Executable: /home/a/andreyolegovichru/.local/bin/python3.7 Python Version: 3.7.0 Python Path: ['/home/a/andreyolegovichru/eth1.ru/public_html/HelloDjango', '/home/a/andreyolegovichru/.local/lib/python3.7/site-packages', '/home/a/andreyolegovichru/eth1.ru', '/opt/passenger40/helper-scripts', '/home/a/andreyolegovichru/.local/lib/python37.zip', '/home/a/andreyolegovichru/.local/lib/python3.7', '/home/a/andreyolegovichru/.local/lib/python3.7/lib-dynload', '/home/a/andreyolegovichru/.local/lib/python3.7/site-packages'] Server time: Sun, 3 Feb 2019 20:07:57 +0000
Check that you entered everything correctly in
settings.py
ALLOWED_HOSTS.
Stop Django, close all consoles connected to the hosting or all consoles on the local machine.
Clear the browser cache or open the url in another browser.
runserver in Django does not work
If you run the command
python3 manage.py runserver
and nothing happens, or for example your very first project worked but you cannot start the second one, the problem is most likely the hosting. It may have the ability to listen on ports disabled, and the working project is chosen by some script.
If, like me, you use
hosting beget
then that script is called
passenger_wsgi.py
and sits at the same level as the public_html directory.
Write in it
os.environ['DJANGO_SETTINGS_MODULE'] = 'Name_of_new_project.settings'
Web application could not be started
If you want to switch between projects and have already updated the passenger_wsgi.py script but got the error
Web application could not be started
You have most likely forgotten to recreate the tmp/restart.txt file
(docker) andreyolegovich@server:~/eth1.ru [0] $ touch tmp/restart.txt
I also advise you to double-check that you did not forget to change the system path to the project you need.
When you change the project you usually have to make two changes in the passenger_wsgi.py file
# -*- coding: utf-8 -*-
import os, sys
sys.path.insert(0, '/home/a/andreyolegovich/eth1.ru/public_html/Project_1')
#sys.path.insert(0, '/home/a/andreyolegovich/eth1.ru/public_html/Project_2')
sys.path.insert(1, '/home/a/andreyolegovich/.local/lib/python3.7/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = 'Project_1.settings'
#os.environ['DJANGO_SETTINGS_MODULE'] = 'Project_2.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Another possible cause is unset variables in the file manage.py
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
python3 manage.py migrate
ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
If you use a virtual environment you most likely do not need the --user flag at all.
Article author: Andrei Olegovich