やりたいこと
Django
本で一通り当たっておきたいと思います。セキュリティも含めてね。
やってみて
データ作成の困難さは→ 仕様がわからないと作りづらいというところにあると思います。
データ投入 ← 日付がすごい大事なのがよくわかりました。もっと追求していきます。
ファイル形式を変える方法については調べられたら調べる。
JSONもっとやっていきます。
完了時点について
タイトルに改訂版をつけた時が完了です
ステータス
まとめのPull編です。
背景
Djangoでものづくりをしながら実務レベルを目指します。
Django Girls のチュートリアルを参考にします。
https://tutorial.djangogirls.org/ja/
仕様
「北天サービスの金融サービス宣伝アプリ」
100万円以上北天サービスに預けると3ヶ月で9%の利子がつきます。
預ける単位は年単位です。
画面設計
北天サービスChat
設計用ノート
myProject
chatApp
money
years
name
OUTPUT
GitHub
HTML部分 jQueryを使用
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$("#submitBtn").click(function(){
$("#myForm").submit(); // Submit the form
});
});
</script>
<div>
<h1>北天サービス1年満期</h1>
<h2>年率36%</h2>
</div>
<h2>New post</h2>
<form method="POST" class="post-form" id="myForm">{% csrf_token %}
{{ form.as_p }}
name <input type="text" name="name" size="20"><br>
money <input type="text" name="money" size="20"><br>
<button type="button" id="submitBtn">Save</button>
</form>
{% for post in posts %}
<div>
<p>{{ post.text}}</p>
</div>
{% endfor %}
TakeAway
postgresにデータアクセスする方法
which postgres
psql -U student postgres
\l
\c db
CREATE DATABASE db
\dt;
pg_ctl -D /usr/local/var/postgres stop
pg_ctl -D /usr/local/var/postgres start
psql -U postgres postgres
pip install psycopg2-binary
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'localhost',
'PORT': 5432,
}
}
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
データ作成
事前準備
staf_a
staf_b
user_a
user_b
上記のキーワードを有効活用してデータのイメージを作って見る。
いわゆるマスター系(アプリ側から作成)
db=# select * from catalog_genre;
id | name
----+-------------------
1 | Science Fiction
2 | Non Fiction
3 | Theatrical script
4 | poem
db=# select * from catalog_language;
id | name
----+----------
1 | English
2 | French
3 | Japanese
4 | Italian
db=# select * from catalog_book;
id | title | summary | isbn | author_id | language_id
----+---------------------------+-----------------------------+---------------+-----------+-------------
1 | All's Well That Ends Well | All's Well That Ends Well\r+| 1234567890123 | 1 | 1
| | All's Well That Ends Well\r+| | |
| | All's Well That Ends Well | | |
2 | Antony and Cleopatra | Antony and Cleopatra | 1234567890124 | 1 | 1
3 | Divine Comedy | Divine Comedy | 1234567890125 | 2 | 4
db=# select * from catalog_author;
id | first_name | last_name | date_of_birth | date_of_death
----+------------+-------------+---------------+---------------
1 | William | Shakespeare | 1564-04-26 | 1616-04-23
2 | Dante | Alighieri | 1265-01-01 | 1321-09-04
3 | cat | dog | 1564-05-07 | 1616-04-23
db=# \d catalog_author
Table "public.catalog_author"
Column | Type | Collation | Nullable | Default
---------------+------------------------+-----------+----------+--------------------------------------------
id | integer | | not null | nextval('catalog_author_id_seq'::regclass)
first_name | character varying(100) | | not null |
last_name | character varying(100) | | not null |
date_of_birth | date | | |
date_of_death | date
データ投入SQL
INSERT INTO catalog_author VALUES(4, 'Lewis', 'carroll', '1832-01-27', '1898-01-14');
処理系
db=# select * from catalog_bookinstance;
id | imprint | due_back | status | book_id | borrower_id
--------------------------------------+-----------+------------+--------+---------+-------------
58dffa34-3ac4-47b6-9dfa-49b79572a84b | Imprint:A | 2021-04-13 | o | 1 | 2
d946c48c-f017-4eae-a48e-75267ba538a3 | Imprint:B | 2021-04-16 | r | 2 | 3
データ移行ノウハウ
問題と対応
・INSERT文を大量に作っても発行するときに失敗起こってしまったりも怖いあやまりです。
・文字化け← Shift_JIS から UTF-8に手動で変換
・INSERT文に関してはあまり各SQL方言ごとに違いはないように見受けられる。
TRANSACTIONがBEGINなのかSTARTという違いぐらい。
・Create文作成が失敗した。← タブを半角に置換して対応
Create文参考例(想定DB Postgres)
CREATE TABLE Item
(item_id CHAR(4) NOT NULL,
item_mei VARCHAR(100) NOT NULL,
item_bunrui VARCHAR(32) NOT NULL,
item_tanka INTEGER ,
shiire_tanka INTEGER ,
torokubi DATE ,
PRIMARY KEY (item_id));
START TRANSACTION;
INSERT INTO Item VALUES ('0001', 'ノート' ,'文房具', 1000, 500, '2009-09-20');
COMMIT;
追加情報
CREATE TABLE item(
id integer,
account_id integer,
occurred_at timestamp,
channel bpchar
);
INSERT INTO item VALUES (1,1001,'2015-10-06 17:13:58','direct');
jQuery
各種cheat sheet
https://qiita.com/JoeB/items/f6847309d4038d576f89
ドットインストール jQuery
https://dotinstall.com/lessons/basic_jquery_v2
jQuery参考書籍
https://www.amazon.co.jp/dp/4048913913?tag=technote012-22&linkCode=ogi&th=1&psc=1
URL設計代用
urlpatterns = [
path('', views.index, name='index'),
path('books/', views.BookListView.as_view(), name='books'),
]