Memo
$django-admin startproject project name
When you create a project, the same directory as the project name is created. Files used throughout the project are saved here.
__inti__.py
A script file that does the initialization when you run your Django project. asgi.py A program for creating asynchronous web applications called ASGI settings.py Describe the project setting information file, add the application name to INSTALLED_APPS [] when creating the application urls.py Manage URLs in your project wsgi.py WSGI, a popular web application program
$python manage.py startapp application name
== Configuration when creating application folder == migrations folder
Files of database related functions are collected __inti__.py File for application initialization process admin.py Files for admin tools apps.py Summarize the processing of the application itself models.py Model processing tests.py Program testing views.py Screen display
== Individually created items == urls.py
Because you need to configure routing for each application templates folder Use the template function (Create a folder with the application name in the templates folder. Link with the application added to settings.py, go to render with the render function) static folder Use static files, create a folder with the application name in the static folder, and use the static template tag to automatically look inside the static folder
$python manage.py runserver
$python manage.py makemigrations application name
$python manage.py migrate
・ Reference URL for connecting to existing Django DB
This time it was necessary to connect to ClearDB (mysql) in the heroku environment, so record it.
settings.py
DATABASES = {
'default': {
#Existing code
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
#herokuDB connection information
'ENGINE': 'django.db.backends.mysql',
'HOST': os.getenv('DB_HOSTNAME'),
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USERNAME'),
'PASSWORD': os.getenv('DB_PASSWORD'),
}
}
■ Connection test OK if you can connect to the DB of the configuration file with the following command
$python manage.py dbshell
■ Confirmation of connection information to herokuDB After logging in to heroku, execute the following command Since environment variables can be listed, the connection information to the DB is in CLEARDB_DATABASE_URL.
$heroku config --app application name
Once the connection test is complete, create a model for use with django.
$python manage.py inspectdb
When you type this command, it will be based on the DB information. The model code is returned. (In this case, it is not saved.)
If the code of the returned model seems to be okay, copy and paste it into the model. Alternatively, a model file is generated by typing the following command.
$python manage.py inspectdb >> models.Path to py/models.py
Recommended Posts