Get current sitename in multi tenant system

Hey there,

I need to know how to access global variables, like the current sitename in ERPNext / Frappe. Unfortunately I did not found any information for this.

This is very basic information, so I think, it should be mentioned somewhere.

Thank you in advance.

1 Like
from frappe.utils import get_site_name
3 Likes

@max_morais_dmm Thank you very much. Unfortunately, this doesn’t solve the problem, since I have to pass a parameter to get_site_name.

I tried to check the repository for the usage of get_site_name, but trying for example frappe.app._site is empty.

I solved my problem as follows, but it’s probably not the perfect solution.

Instead of using get_site_name(), I used get_site_base_path(), which doesn’t need any parameters. The result was for example like the following

./erp.mysite.local

Of course, it’s not getting the site_name, but for my needs it was perfect. Maybe someone can tell us how to get erp.mysite.local instead, without having to slice the string.

Hi,

Try this
get_site_name(frappe.local.request.host)

Only if the domain name and site name are same.

I didn’t try, but I am sure it would work as well.

I just didn’t know the path to the request.

Works!

Full solution:

import get_site_name method

from frappe.utils import get_site_name

get the site_name

site_name = get_site_name(frappe.local.request.host)

2 Likes

I got to the point, that I need the full url with a prefix.

If the site is ssl secured, it has to be https, if it’s not, it has to be http. If API requests go to http, the reply on a https site is moved permanently, which will lead to the fact, that the api call will not be successful.

Any ideas?

Note about this:

This will not work, if your code is executed by for example a scheduled event. For that, I still have to use:

get_site_base_path()[2:]

Better solutions are appreciated.

Below is the solution to get site name for ERP.

from frappe.utils import cstr

get the site_name
site_name = cstr(frappe.local.site)

7 Likes

Works well, thank you

1 Like

frappe.get_url

Get url of the current site via frappe.utils.get_url