django 001|MTVモード


故障はMTV方式




template:ユーザーが実際に見た部分
モデル:データ(db)
view:データを処理する場所

実習


(1)仮想環境の作成
python -m venv myvenv
(2)仮想環境の実行
source myvenv/Scripts/activate
(3)取付故障
pip install django
(4)プロジェクトの作成
django-admin startapp myproject
(5)プロジェクトへ移動
cd myproject
(6)アプリケーションの作成
app:djangoプロジェクトに使用される小さな単位
'
(NAVERはプロジェクト、appはその機能)
python manage.py startapp firstapp
=>プロジェクトにappフォルダを作成
(7)アプリケーション設定を追加する.pyにappを追加する必要があります(appをapp 1と命名し、「app 1.apps.app 1 Config」を追加します)
(アプリケーション名.apps.appアプリケーション名(ただし大文字)+Config)

===>Webサイトの駆動順序

=>テンプレートを作成し、=>viewsで処理する関数を作成します.
(8)そのため、ユーザーに=>どこにあるかを表示するスタンプを作成する必要があります.アプリケーション内
  • で作成したappを右クリックし、新しいフォルダを作成しtemplatesと名前を付けます.
    (9)templatesフォルダ内にhtmlを作成する
    (10)装飾html
    <!DOCTYPE html>
    <html lang="kor">
    <head>
        <meta charset="UTF-8">
    
        <title>Document</title>
    </head>
    <body>
        <div style="text-align: center;">
            <h1>사용자의 이름을 입력하시오</h1><br>
            <form action="">
                <label for="nameInput">이름</label>
                <input id="nameInput" name="name">
                <input type="submit" value="이름">
            </form>
        </div>
    </body>
    </html>
    =>form機能:https://velog.io/@myway00/HTML-Form
    (11)テンプレートを作成した場合は、このコマンドを処理する関数を参照してください.pyに入力
    from django.shortcuts import render
    def home(request):
        return render(request, "home.html")
    (12)元のモデル(db)部分も処理するのか、今はもう処理できる部分がないので、今回はurlに接続するのでurls.尹秀晶.
    ビューと
  • urlを接続するには、次の手順に従います.
    Function views
    1. Add an import: from my_app import views
    2. Add a URL to urlpatterns: path('', views.home, name='home')
    Class-based views
    1. Add an import: from other_app.views import Home
    2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
    Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns: path('blog/', include('blog.urls')
  • (12-1 appのviewsをインポート)
    from app1 import views
  • の上部に追加
  • (12~2パス追加)
     path('',views.home, name="home"), #name을 씀으로써 url을 저 name으로 대체가능 - 나중에 html에서 action="url name" 적어주면 그것 누르면 이동 가능
    (13)今回名前を入力して移動した結果.html=>views=>url接続順序
    (13-1)
    result.html
    <div style="text-align: center;">
    
        <h1>반갑습니다 {{userName}}님~</h1>
    
    </div>
    
    (+)Python言語などを使用しなければならない場合{{}スタンプ言語!
    (13-2) views
    def result(request):
        userName =  request.GET['name']
        return render(request, "result.html",{"userName" : userName}) #키 : 값
    =>template受信「name」
    => result.userName値をキーワード「userName」としてhtml~=>に渡し、{}に入れて提示します.伝達する.に与える
    (13-3) urls
    path('result/', views.result, name="result"),
    (13-4)urlが正しく指定されている場合はhtml form=action「url name」と入力します.
    このフォームで「Submit」(Submit)をクリックすると、結果が得られます.htmlに移動するので、アクションで結果を指定できます.

    結果ウィンドウ


    (1)トップページ.html

    (1-1)name=Mad Monster

    (2)提出後の結果をクリックする.html(名前値の転送が良好)

    (+)
    urlアドレスの変更
    (1) http://127.0.0.1:8000/
    (2) http://127.0.0.1:8000/result/?name=%EB%A7%A4%EB%93%9C%EB%AA%AC%EC%8A%A4%ED%84%B0