Bad Request (400) using Django, Heroku, and Name.com
Asked Answered
R

2

14

I just deployed my Django site with Heroku and it works at example.herokuapp.com just fine.

The problem I'm having is when I go to example.com, I get Bad Request (400). I've looked at the Django documentation about 400 errors and I can't seem to figure out whats wrong.

I'm using Django 1.9.5 and started the project with cookiecutter-django. Name.com is my domain registrar.

production.py of my settings:

ALLOWED_HOSTS = env.list('DJANGO_ALLOWED_HOSTS', default=['example.com'])

I'm not 100% sure how this DJANGO_ALLOWED_HOSTS part of my ALLOWED_HOSTS works, but when I go to my Heroku dashboard and look at my config variables in settings, I see that DJANGO_ALLOWED_HOSTS is .herokuapp.com

Heroku custom domains:

Domain Name            DNS Target
*.example.com          example.herokuapp.com
example.com            example.herokuapp.com
www.example.com        example.herokuapp.com

Name.com DNS Records:

Type   Host             Answer                 TTL  Prio
CNAME  www.example.com  example.herokuapp.com  300  N/A
CNAME  example.com      example.herokuapp.com  300  N/A
CNAME  *.example.com    example.herokuapp.com  300  N/A

In the console I type the host command I get:

$ host www.example.com
www.example.com is an alias for example.herokuapp.com

$ host example.com
example is an alias for example.herokuapp.com

Most answers/forums I can find point at these 3 places to find the error, ALLOWED_HOSTS, Heroku custom domains, and DNS records. But I just can't spot the error.

One suggestion I've found stated to check other areas of the site. I get the same error no matter where I go, such as example.com/about/

Do I need to set something else up at name.com?

Retread answered 9/4, 2016 at 18:7 Comment(1)
G
22

Just add .example.com to ALLOWED_HOSTS setting using your command line:

heroku config:set DJANGO_ALLOWED_HOSTS=.example.com,example.herokuapp.com

or using the heroku dashboard.

Graiae answered 9/4, 2016 at 23:11 Comment(2)
That did the trick. At first your suggestion did not work because I included a space between them, then I noticed you did not have a space and it worked! Just noting this in case someone else makes the same mistake I did.Retread
Totally forgot about that setting, thanks +1Abuzz
H
1

one line solution

#ALLOWED_HOSTS = ['localhost', '127.0.0.1','https://tm-sys-dashbobord.herokuapp.com/', config('SERVER', default='127.0.0.1')]

to replace

ALLOWED_HOSTS =['*'] 
Hinch answered 18/7, 2021 at 17:48 Comment(1)
This does adresses the problem, but its not a good idea from a security point of view, since it allow any host to access the end-points. I would recommend it only if the aplication is an open API or on tests for deployment.Kennedy

© 2022 - 2024 — McMap. All rights reserved.