ERPNext Conference 2019* ERPNext.com Blog

Get current sitename in multi tenant system


#1

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.


Changes made to py file show on all sites
#2
from frappe.utils import get_site_name

#3

@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.


#4

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.


#5

Hi,

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

Only if the domain name and site name are same.


#6

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

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


#7

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)


Is there function returns the current site url
#8

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?


#9

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.


#10

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)


#11

Works well, thank you