日和見軽太郎商会のブログ -3ページ目

日和見軽太郎商会のブログ

今日はヒマだし天気もイイし、クルマでもちょすとしますか!

$ source ~/django/bin/activate

 

新規プロジェクト作成
$ django-admin startproject TestProject

すると、ディレクトリ「TestProject」が作られた
$ ls -la TestProject
合計 8
drwxr-xr-x ? 3 his his ? 35 12月 22 14:02 .
drwx------. 22 his his 4096 12月 22 14:02 ..
-rwxr-xr-x ? 1 his his ?660 12月 22 14:02 manage.py
drwxr-xr-x ? 2 his his ? 89 12月 22 14:02 TestProject

更に、ディレクトリ「TestProject」の中の「TestProject」は
$ ls -la TestProject/TestProject
合計 16
drwxr-xr-x 2 his his ? 89 12月 22 14:02 .
drwxr-xr-x 3 his his ? 35 12月 22 14:02 ..
-rw-r--r-- 1 his his ? ?0 12月 22 14:02 __init__.py
-rw-r--r-- 1 his his ?385 12月 22 14:02 asgi.py
-rw-r--r-- 1 his his 3215 12月 22 14:02 settings.py
-rw-r--r-- 1 his his ?746 12月 22 14:02 urls.py
-rw-r--r-- 1 his his ?385 12月 22 14:02 wsgi.py

 

「settings.py」を編集して環境を構成

デフォルトでは他のPCからアクセス不可の様子なので

「ALLOWED_HOSTS = []」を「ALLOWED_HOSTS = ['*']」

と変更

 

デフォルトのデータベースがSqlite3なのでPostgreSQLへ変更

変更前
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

 

変更後
DATABASES = {
    'default': {
        ##'ENGINE': 'django.db.backends.sqlite3',
        ##'NAME': BASE_DIR / 'db.sqlite3',
        ##'ENGINE': 'django.db.backends.postgresql_psycopg2', #どちらが正解?
        'ENGINE': 'django.db.backends.postgresql', #どちらが正解?
        'NAME': 'DB名',
        'USER': 'UID',
        'PASSWORD': 'P/W',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

 

それと
「LANGUAGE_CODE = 'en-us'」を「LANGUAGE_CODE = 'ja-jp'」
「TIME_ZONE = 'UTC'」を「TIME_ZONE = 'Asia/Tokyo'」

 

DB「dbtest」を作成して

$ cd TestProject

$ python manage.py migrate

 

DB「dbtest」内にテーブルと連番制御用のシークエンスが作成された

 

スーパーユーザーを作成?

$ python manage.py createsuperuser
Username (leave blank to use 'UID'):
Email address:
Password:
Password (again):
Superuser created successfully.

現在作業中のOSのユーザーで作成された様子

 

ポート開放

$ sudo firewall-cmd --zone=public --add-port=8000/tcp

$ sudo firewall-cmd --zone=public --add-port=8000/tcp --permanent

$ sudo firewall-cmd --reload

 

サーバー起動?

$ python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
December 22, 2022 - 05:59:31
Django version 4.1.4, using settings 'TestProject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

 

で、他のPCでブラウザを起動して「http://サーバーIPアドレス:8000」

「Django」のサイトが表示されるのでヨシ?

 

で、「http://サーバーIPアドレス:8000/admin」から管理画面表示

 

ココまで

CTRL+C

$ deactivate

 

$ python3 -m venv --system-site-packages ~/django

$ source ~/django/bin/activate

$ pip3 install Django==4.1.4

 

インストールできたか確認

$ django-admin --version

 

先ずはココまで

$ deactivate

で、ついでに更新のストアドプロシジャ実行のソースも変更

#!/usr/bin/env python3
# import sys
import psycopg2
import psycopg2.extras
import datetime

from sqlalchemy import create_engine
from sshtunnel import SSHTunnelForwarder

print('START : ' + '{0:%Y/%m/%d %H:%M:%S.%f}'.format(datetime.datetime.now()))
try:
    server = SSHTunnelForwarder((SV名, 22), ssh_username=UID, ssh_password=P/W, ssh_private_key_password=p/w, ssh_pkey=秘密鍵, remote_bind_address=('127.0.0.1', 5432))
    try:
        server.start()

        try:
            strCon = "postgresql+psycopg2://UID:P/W@127.0.0.1:" + '{0:5d}'.format(server.local_bind_port) + "/DB名"
            print(strCon)
            engine = create_engine(strCon, client_encoding="utf8")
            try:
                con = engine.raw_connection()
                ##con = psycopg2.connect(host = SV名, port = 5432, database = DB名, user = UID, password = P/W)

                try:
                    cur = con.cursor(cursor_factory = psycopg2.extras.DictCursor)

                    try:
                        intRet = -1
                        cur.execute("call pr_mst_item_entry_00 (2, 12345678, '上下セット', 'ジョウゲセット', 5432, 12000, 'WASHI', 'COMPUTER_NAME', 999, ARRAY[100,2000,30000], ARRAY['A','B','C'], -9)")
                        ##rows = cur.fetchall()
                        row = cur.fetchone()

                        ##for row in rows:
                        for intValue in row['p_num']:
                            print('{0:+09}'.format(intValue))

                        for strValue in row['p_msg']:
                            print(strValue)

                        intRet = row['p_result']

                        if intRet == 0:
                            con.commit()
                            print("OK : Commit")

                        else:
                            con.rollback()
                            print("NG : Rollback")

                    except (Exception, psycopg2.DatabaseError) as err:
                        print(err)
                        con.rollback()

                    except Exception as err:
                        print(err)
                        con.rollback()

                    finally:
                        cur.close()

                except (Exception, psycopg2.DatabaseError) as err:
                    print(err)

                except Exception as err:
                    print(err)

                finally:
                    con.close()


            except Exception as err:
                print(err)

        except Exception as err:
            print(err)

        finally:
            server.close()

    except Exception as err:
        print(err)

except Exception as err:
    print(err)

finally:
    print('FINISH : ' + '{0:%Y/%m/%d %H:%M:%S.%f}'.format(datetime.datetime.now()))