Django and Bad Request (400)

谁说胖子不能爱 提交于 2019-12-05 04:30:00

问题


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?


回答1:


It might be a bad Cookie. Try deleting them.




回答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.




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/25357744/django-and-bad-request-400

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!