Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the get the port of the instance in Django settings

Tags:

django

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.

like image 959
cem Avatar asked Oct 31 '13 13:10

cem


People also ask

What is the port number of Django?

By default, the server runs on port 8000 on the IP address 127.0. 0.1 .

How do I change my Django port number?

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"

What is Allowed_hosts in Django settings?

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.

How do I change Django settings?

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.


2 Answers

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)
like image 147
x-yuri Avatar answered Oct 13 '22 18:10

x-yuri


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']
like image 24
Matt Seymour Avatar answered Oct 13 '22 18:10

Matt Seymour