Есть ли способ импортировать файл в forms.py или views.py?
В моем проекте Django я создал подпроекцию
country/translations
где country - название приложения, translations - папка.
Я создал
__init__.py
внутри него, чтобы Python распознал его как модуль, и внутри него я создал файл countries_fr.py.
Внутри файла countries_fr.py у меня есть эта пользовательская функция перевода:
def t_countries(c):
country_dict = {
"Albania" : "Albanie",
"Algeria" : "Algérie",
"Antigua and Barbuda" : "Antigua-et-Barbuda",
"Argentina" : "Argentine",
"Armenia" : "Arménie",
# etc.
}
if c in country_dict:
return country_dict[c]
return c
Я не использую gettext в этом случае, потому что список стран поступает из json файла, а gettext не переводит данные, поступающие из баз данных или файлов.
Я хочу использовать его в Forms.py таким образом
def get_country():
filepath = 'myproj/static/data/countries_states_cities.json'
all_data = readJson(filepath)
all_countries = [('----', _("--- Select a Country ---"))]
for x in all_data:
y = (x['name'], t_countries(x['name']))
all_countries.append(y)
return all_countries
Проблема в том, что я не могу импортировать пресловутую функцию, я пробовал все это
from .translations/countries_fr import t_countries
from .translations.countries_fr import t_countries
from translations.countries_fr import t_countries
from translations import t_countries
from translations import countries_fr
from .translations import countries_fr
from translations import t_countries
from countries.translations import countries_fr
from countries.translations.countries_fr import t_countries
import countries.translations
and more
У меня нет НИКАКОГО способа распознать этот пресловутый файл.
Может ли быть незаконным импорт пользовательских вещей в файлах типа forms.py?
Каким образом правильно импортировать файл и использовать функцию, в которой я отчаянно нуждаюсь, в файле forms.py?