Is there any way to get which port Django is working on in settings? I am trying to find it in socket but I couldn't manage to find it.
By default, the server runs on port 8000 on the IP address 127.0. 0.1 .
Inside the commands folder open up the runserver.py script with a text editor. Find the DEFAULT_PORT field. it is equal to 8000 by default. Change it to whatever you like DEFAULT_PORT = "8080"
ALLOWED_HOSTS. A list of strings representing the host/domain names that this Django site can serve. This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly-safe web server configurations.
Default settings These defaults live in the module django/conf/global_settings.py . Here's the algorithm Django uses in compiling settings: Load settings from global_settings.py . Load settings from the specified settings file, overriding the global settings as necessary.
The closest you can get is probably:
import django.core.management.commands.runserver as runserver
cmd = runserver.Command()
print('http://' + cmd.default_addr + ':' + cmd.default_port)
Dont know if this is what you want but I found this in the django documentation.
https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META
Check out SERVER_PORT
.
It can be accessed inside of a view by using:
port_number = request.META['SERVER_PORT']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With