[Django] How to read variables / constants defined in an external file

It is a memo First, define variables / constants with the file name consts.py Here, consts.py is placed directly under the application directory.

project/application/consts.py


PREF_CHOICE = (
    ('', 'Prefectures'),
    ('1', 'Hokkaido'),
    ('2', 'Aomori Prefecture'),
    ('3', 'Iwate Prefecture'),
    ('4', 'Miyagi Prefecture'),
    ('5', 'Akita'),
    ('6', 'Yamagata Prefecture'),
    ('7', 'Fukushima Prefecture'),
    ('8', 'Ibaraki Prefecture'),
    ('9', 'Tochigi Prefecture'),
    ('10', 'Gunma Prefecture'),
    ('11', 'Saitama'),
    ('12', 'Chiba'),
    ('13', 'Tokyo'),
    ('14', 'Kanagawa Prefecture'),
    ('15', 'Niigata Prefecture'),
    ('16', 'Toyama Prefecture'),
    ('17', 'Ishikawa Prefecture'),
    ('18', 'Fukui prefecture'),
    ('19', 'Yamanashi Prefecture'),
    ('20', 'Nagano Prefecture'),
    ('21', 'Gifu Prefecture'),
    ('22', 'Shizuoka Prefecture'),
    ('23', 'Aichi prefecture'),
    ('24', 'Mie Prefecture'),
    ('25', 'Shiga Prefecture'),
    ('26', 'Kyoto'),
    ('27', 'Osaka prefecture'),
    ('28', 'Hyogo prefecture'),
    ('29', 'Nara Prefecture'),
    ('30', 'Wakayama Prefecture'),
    ('31', 'Tottori prefecture'),
    ('32', 'Shimane Prefecture'),
    ('33', 'Okayama Prefecture'),
    ('34', 'Hiroshima Prefecture'),
    ('35', 'Yamaguchi Prefecture'),
    ('36', 'Tokushima Prefecture'),
    ('37', 'Kagawa Prefecture'),
    ('38', 'Ehime Prefecture'),
    ('39', 'Kochi Prefecture'),
    ('40', 'Fukuoka Prefecture'),
    ('41', 'Saga Prefecture'),
    ('42', 'Nagasaki Prefecture'),
    ('43', 'Kumamoto Prefecture'),
    ('44', 'Oita Prefecture'),
    ('45', 'Miyazaki prefecture'),
    ('46', 'Kagoshima'),
    ('47', 'Okinawa Prefecture'),
)



Load this in views.py or forms.py as follows

project/application/views.py


from .consts import PREF_CHOICE

....

Now the variables / constants defined in consts.py are available If you have defined multiple variables / constants in consts.py and want to use them all in views.py or forms.py, you can use them with from .consts import *.

Recommended Posts

[Django] How to read variables / constants defined in an external file
How to read a file in a different directory
How to read environment variables from .env file in PyCharm (on Mac)
Read logging settings from an external file in Flask
How to check ORM behavior in one file with django
How to read an Excel file (.xlsx) with Pandas [Python]
How to reflect CSS in Django
How to access environment variables in Python
How to dynamically define variables in Python
python3 How to install an external module
How to convert Python to an exe file
How to delete expired sessions in Django
How to do Server-Sent Events in Django
How to convert DateTimeField format in Django
How to read CSV files in Pandas
How to read a CSV file with Python 2/3
How to implement Rails helper-like functionality in Django
How to create a JSON file in Python
[Python] How to read excel file with pandas
How to reflect ImageField in Django + Docker (pillow)
How to run some script regularly in Django
How to get help in an interactive shell
How to read an array with Python's ConfigParser
How to read time series data in PyTorch
How to create a Rest Api in Django
How to get people to try out django rest framework features in one file
Django template reads Makdown and reStructuredText written in an external file as HTML
How to read a serial number file in a loop, process it, and graph it
How to read PyPI
Backtrader How to import an indicator from another file
How to read JSON
How to use bootstrap in Django generic class view
How to use template engine in pyramid 1 file application
How to make an interactive CLI tool in Golang
How to turn a .py file into an .exe file
How to use variables in systemd Unit definition files
Julia Quick Note [01] How to use variables and constants
How to create an image uploader in Bottle (Python)
How to upload files in Django generic class view
[Work efficiency] How to change file names in Python
How to use Decorator in Django and how to make it
How to pass matplotlib backend settings in environment variables
How to reference static files in a Django project
How to read csv containing only integers in Python
How to read text by standard input or file name specification like cat in Python
How to write custom validations in the Django REST Framework
How to switch the configuration file to be read by Python
How to use Laravel-like ORM / query builder Orator in Django
How to swap elements in an array in Python, and how to reverse an array.
How to update user information when logging in to Django RemoteUserMiddleware
How to import a file anywhere you like in Python
[Python] How to write an if statement in one sentence.
How to define multiple variables in a python for statement
[Django] How to give input values in advance with ModelForm
How to manipulate the DOM in an iframe with Selenium
How to generate a query using the IN operator in Django
How to check if a value exists in an enum
[Python] How to change EXCEL file saved in xlsb to xlsx
In Django, how to abbreviate the long displayed string as ....
[Python Kivy] How to create an exe file with pyinstaller
[Python] How to output a pandas table to an excel file