4

I created new django project; added to my settings.py:

DEBUG = False
ALLOWED_HOSTS= [
    'localhost',
    'my_site.com'
]

created app test_view; added hello_world to test_view.views

from django.http.response import HttpResponse

def hello_world(request):
    return HttpResponse('Hello World!!!')

added test route to urls.py url(r'test/', 'test_view.views.hello_world'); fixed /etc/hosts

127.0.0.1    localhost my_site.com

Now when i'm trying to access http://my_site.com:8000/test/ django returns Bad Request (400). But when url is http://localhost:8000/test/ I can see my Hello World page. What can be wrong?

UPD: The same result with DEBUG = True

UPD2:

One more working hostname is ubuntu-virtualbox (computer's name). But even when I changed computer's name to my_site, ubuntu-virtualbox was still available and my_site returned Bad Request (400)

May it be because of some system settings? (it's clean ubuntu in virtualbox). Or maybe problem in virtualenv? Is there a way to trace the error?

Anton
  • 2,217
  • 3
  • 21
  • 34

4 Answers4

3

It might be a bad Cookie. Try deleting them.

sbaechler
  • 1,329
  • 14
  • 21
2

It looks like django can see if request isn't passed through dns server. Installation and configuration of bind9 instead of changes in /etc/hosts solved this problem.

Anton
  • 2,217
  • 3
  • 21
  • 34
1

You need another line in your hosts file.

127.0.0.1     localhost
127.0.0.1     my_site.com

Then in your ALLOWED_HOSTS...

ALLOWED_HOSTS = [
    'localhost',
    '.my_site.com', # not 'my_site.com'
]

ALSO, and this is probably important seeing as you are running your site from a virtual machine, when you run the site with python manage.py runserver, run it like this...

python manage.py runserver virtual.server.ip.address:8000

Obviously replace 'virtual.server.ip.address' with that virtual machines actual ip address.

teewuane
  • 5,524
  • 5
  • 37
  • 43
  • It says `Error: That IP address can't be assigned to` error – Saras Arya Jan 30 '17 at 09:59
  • @SarasArya try to run the development server like this `python manage.py runserver 0.0.0.0:8000` That will basically have it run on whatever your current ip address is. You should still be able to access the site from `localhost:8000` or `127.0.0.1:8000`. I hope that helps. – teewuane Jan 30 '17 at 21:45
-1

I print *DEBUG = None* and my django works.

Maurizio In denmark
  • 4,226
  • 2
  • 30
  • 64