Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django allowed hosts with port number

Tags:

python

django

I use django as a backend and it's running on 8000 port at the loopback interface. So, when I'am to try start it with DEBUG = False, I got 500 error on any request from a frontend. I set my ALLOWED_HOSTS as:

 ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '127.0.0.1:8000', 'localhost:8000', '*',]

But this doesn't work for me. Is it possible disable this option or how I can do that?

UPDATE So I just declared variable ALLOWED_HOSTS above of default ALLOWED_HOSTS = []. Sorry for the inattention.

like image 477
Denis Avatar asked Nov 07 '13 09:11

Denis


People also ask

How to set default port number for Django runserver?

I guess it can help you. when you run python manage.py runserver, it will take 127.0.0.1 as default ip address and 8000 as default port number which can be configured in your python environment. In your python setting, go to <your python env>\Lib\site-packages\django\core\management\commandsunserver.pyand set 1. default_port = '<your_port>'

What is allowed_hosts in Django?

It simply mentions the list of address from where your site can be accessed. like ALLOWED_HOSTS = ['your_site.com', 'IP_ADDRESS_OF_YOUR_SITE'] for more information visit docs And for why ['*'] being dangerous and why ALLOWED_HOST was added to django please refer to this post.

How do I import runserver settings in Django?

Save the file as a management command of your own, e.g. under <app-name>/management/commands/runserver.py: from django.conf import settings from django.core.management.commands import runserver class Command(runserver.Command): default_port = settings.RUNSERVER_PORT

What is a cache dictionary in Django?

A dictionary containing the settings for all caches to be used with Django. It is a nested dictionary whose contents maps cache aliases to a dictionary containing the options for an individual cache. The CACHES setting must configure a default cache; any number of additional caches may also be specified.


1 Answers

Normally, improper django ALLOWED_HOSTS should lead to ”Bad Request (400)”.

In more details, DisallowedHost (child class to SuspitiousOperation) is raised by request in HttpRequest.get_host(), and is processed later by request hadler, returning 400 HTTP response. You might get 500 error if an exception is occured in resolver.resolve400().

@Denis may be you mangled ALLOWED_HOSTS. I'd suggest you debug its value (logging it for example). See how validation works, your '*' should skip any host validation

like image 53
alko Avatar answered Oct 13 '22 00:10

alko