111π
I generally put something like this in settings.py
:
import socket
try:
HOSTNAME = socket.gethostname()
except:
HOSTNAME = 'localhost'
85π
If you have a request (e.g., this is inside a view), you can look at request.get_host()
which gets you a complete locname (host and port), taking into account reverse proxy headers if any. If you donβt have a request, you should configure the hostname somewhere in your settings. Just looking at the system hostname can be ambiguous in a lot of cases, virtual hosts being the most common.
- [Django]-Django switching, for a block of code, switch the language so translations are done in one language
- [Django]-Simple guestbook django: __init__() takes 1 positional argument but 2 were given
- [Django]-Django: Group by date (day, month, year)
10π
If you need to get http(s)://hostname/
you can use the following:
request.build_absolute_uri('/')
All useful methods are listed here
- [Django]-How do I return JSON without using a template in Django?
- [Django]-Setting default value for Foreign Key attribute in Django
- [Django]-Django: Catching Integrity Error and showing a customized message using template
8π
Just add to @Tobuβs answer.
If you have a request object, and you would like to know the protocol (i.e. http / https), you can use request.scheme (as suggested by @RyneEverettβs comment).
Alternatively, you can do (original answer below):
if request.is_secure():
protocol = 'https'
else:
protocol = 'http'
Because is_secure() returns True
if request was made with HTTPS.
- [Django]-Complete django DB reset
- [Django]-How to send a correct authorization header for basic authentication
- [Django]-Django+Postgres: "current transaction is aborted, commands ignored until end of transaction block"
- [Django]-You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application
- [Django]-Django TypeError: 'RelatedManager' object is not iterable
- [Django]-Any way to make {% extends 'β¦' %} conditional? β Django
4π
Basically, You can take with request.get_host()
in your view/viewset. It returns <ip:port>
- [Django]-Get object by field other than primary key
- [Django]-What is the best location to put templates in django project?
- [Django]-Django: Reference to an outer query may only be used in a subquery
4π
If you have a request object, you can use this function:
def get_current_host(self, request: Request) -> str:
scheme = request.is_secure() and "https" or "http"
return f'{scheme}://{request.get_host()}/'
- [Django]-Django REST Framework (DRF): Set current user id as field value
- [Django]-How can I find the union of two Django querysets?
- [Django]-Django south migration β Adding FULLTEXT indexes
- [Django]-Django error: render_to_response() got an unexpected keyword argument 'context_instance'
- [Django]-How to set True as default value for BooleanField on Django?
- [Django]-Get list item dynamically in django templates
-1π
To get my django server name I tried this
host = f"{ request.scheme }://{ request.META.get('REMOTE_ADDR') }"
- [Django]-How do you get PyPy, Django and PostgreSQL to work together?
- [Django]-Filtering dropdown values in django admin
- [Django]-Django AutoField with primary_key vs default pk