When i run the server it shows me a message http://127.0.0.1:8000/. I want to get the url in code. I dont have request object there.
当我运行服务器时,它会显示一条消息http://127.0.0.1:8000/。我想在代码中获取url。我那里没有请求对象。
5 个解决方案
#1
17
request.build_absolute_uri('/')
or you could try
或者你可以试试
import socket
socket.gethostbyname(socket.gethostname())
#2
11
With Django docs here, You can use:
使用Django文档,您可以使用:
domain = request.get_host()
# domain = 'localhost:8000'
#3
2
If you want to know exactly the IP address that the development server is started with, you can use sys.argv
for this. The Django development server uses the same trick internally to restart the development server with the same arguments
如果您想确切知道开发服务器的IP地址,可以使用sys.argv。 Django开发服务器在内部使用相同的技巧来重新启动具有相同参数的开发服务器
Start development server:
启动开发服务器:
manage.py runserver 127.0.0.1:8000
Get address in code:
在代码中获取地址:
if settings.DEBUG:
import sys
print sys.argv[-1]
This prints 127.0.0.1:8000
这打印127.0.0.1:8000
#4
0
import socket
host = socket.gethostname()
#5
0
import re
import subprocess
def get_ip_machine():
process = subprocess.Popen(['ifconfig'], stdout=subprocess.PIPE)
ip_regex = re.compile('(((1?[0-9]{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((1?[0-9]{1,2})|(2[0-4]\d)|(25[0-5]))')
return ip_regex.search(process.stdout.read(), re.MULTILINE).group()
#1
17
request.build_absolute_uri('/')
or you could try
或者你可以试试
import socket
socket.gethostbyname(socket.gethostname())
#2
11
With Django docs here, You can use:
使用Django文档,您可以使用:
domain = request.get_host()
# domain = 'localhost:8000'
#3
2
If you want to know exactly the IP address that the development server is started with, you can use sys.argv
for this. The Django development server uses the same trick internally to restart the development server with the same arguments
如果您想确切知道开发服务器的IP地址,可以使用sys.argv。 Django开发服务器在内部使用相同的技巧来重新启动具有相同参数的开发服务器
Start development server:
启动开发服务器:
manage.py runserver 127.0.0.1:8000
Get address in code:
在代码中获取地址:
if settings.DEBUG:
import sys
print sys.argv[-1]
This prints 127.0.0.1:8000
这打印127.0.0.1:8000
#4
0
import socket
host = socket.gethostname()
#5
0
import re
import subprocess
def get_ip_machine():
process = subprocess.Popen(['ifconfig'], stdout=subprocess.PIPE)
ip_regex = re.compile('(((1?[0-9]{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((1?[0-9]{1,2})|(2[0-4]\d)|(25[0-5]))')
return ip_regex.search(process.stdout.read(), re.MULTILINE).group()