Last active
June 28, 2023 16:23
-
-
Save eezis/7586964 to your computer and use it in GitHub Desktop.
Resolving Bad Request (400) when using Django on an EC2 instance DEBUG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This gist pertains to a Bad Request(400) error when trying to access an EC2 instance running django . . . | |
I set up an EC2 server, installed django, started runserver and tested. The result was | |
Bad Request (400) | |
When using Django 1.5 and higher, if you specify DEBUG=False in the settings.py file, you must also specify ALLOWED_HOSTS | |
or you will recieve a "Bad Request (400)" error message when attempting to retrieve a page. | |
On an Amazone EC2 instance, if you are ***testing*** with the public DNS, then you should include the public DNS as one of the allowed hosts. | |
--- example --- | |
DEBUG = False | |
ALLOWED_HOSTS = ['.yourdomain.com', 'ec2-XX-XX-XX-XXX.us-west-2.compute.amazonaws.com',] | |
--- end --- | |
If have SSH'ed into the server and wish to perform on-server testing you should include 'localhost' as well: | |
python manage.py runserver 0:8000 | |
then . . . | |
curl http://localhost:8000/ | |
or . . . | |
telnet localhost 8000 (then type GET / and hit enter twice) | |
If that does not work add 'localhost' to your ALLOWED_HOSTS file. | |
ALLOWED_HOSTS = ['.yourdomain.com', 'ec2-XX-XX-XX-XXX.us-west-2.compute.amazonaws.com', 'localhost',] | |
======== | |
NOTE WELL: If you reboot your EC2 instance, when it reboots it will have a different public DNS and you | |
will need to update your ALLOWED_HOSTS accordingly. | |
See the 1.5 release notes for more: | |
https://docs.djangoproject.com/en/dev/releases/1.5/#allowed-hosts-required-in-production | |
Hmm I'm also having such an issue. Why four comments in the past month after no comments for a year? Maybe an update broke something?
After noticing the problem, I upgraded my system packages (apt-get dist-upgrade
because it's Debian) and all of the relevant Python packages and still have the problem.
+1
I too had this issue and solved it by adding following to wsgi.py
import sys, os
sys.path.insert(0, '/opt/python/current/app') # I use ElasticBeanstalk, modify to your project path
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your.settings')
I got 400 error, because I forget to import a model class in views.
+1
+1
+1
+1
+1
+1
+1
+1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i'm the same problem as well even with ALLOWED_HOSTS = ['*'] someone needs to post a solution soon