行雲者研發基地
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: 什麼是 Django? tags: 研究, 網頁組, 學習筆記 --- [TOC] # 認識 Django ## 哈哈被棄坑囉! ## Django 簡介 --- Django 是一個基於 Python 語言所開發出的 Web 框架,跟之前介紹的 CodeIgniter 一樣目的是為了讓開發或是維護程式更方便,但相較於後者的 MVC 架構,Django 也有屬於它的 MTV 架構 官網連結:[Django](https://www.djangoproject.com/) ## Django 優點 --- - 基於 Python 語言特性,套件安裝快速容易,且容易學習 - 自帶後台管理系統 admin ,只需要通過簡單的幾行配置和程式碼就可以實現一個完整的後臺資料管理控制平臺。 - debug 資訊詳盡,很容易找出程式碼錯誤所在。 - 內建本地型輕量的 Web 伺服器,可以快速的開啟測試網站。 ## 關於 MTV 及 MVC --- 在上網查了許多資料後主要有分為兩種說法 - 第一種: - MVC 跟 MTV 是相互對應的,如下表 ![](https://i.imgur.com/o3KBOls.png) https://ithelp.ithome.com.tw/articles/10190956 - 第二種: - MVC 的 V 對應到 MTV 裡的 T 以及 V - MVC 的 C 變成了 Django 框架本身 ![](https://i.imgur.com/5JK4wnb.png) https://mropengate.blogspot.com/2015/08/mvcdjangomtv.html # 額外補充 ## Pylint --- ### Pylint 介紹 --- Pylint 這個套件相當於 Python 版的 phpcs 如果不知道 phpcs 是甚麼的話 [PHP_CodeSniffer](https://hackmd.io/2C4YNmMcThejlgYSN8GDiQ) 這邊簡單講解一下 Pylint 這套件主要是幫你找出不符合 Coding Style 的地方 像 PHP 依據的 Coding Style 是 PSR 而 Python 自己也有他的 Coding Style PEP 假如對為什麼要遵守 Coding Style 有疑問的話 [什麼是Coding style?](https://hackmd.io/nlmGGR2qThC3YBjCVKtzUQ) ### Pylint 使用 --- 首先要去安裝 Pylint 這套件 而 Python 安裝的方法就是去下 `` pip install pylint`` 因為這次我們有使用 Django 所以也要下 ``pip install pylint_django `` ![](https://i.imgur.com/4G8cvLP.png) 下載好後要去更改 settings.json 的內容 ``` json= { "python.linting.enabled": true, "python.linting.pylintEnabled": true, "python.linting.pylintArgs": [ "--load-plugins=pylint_django", "--disable=django-not-configured", "--django-settings-module=主資料夾名稱(有 setting.py 的).settings", ], ``` Pylint 所控管的 Coding Style 中有需要對檔案、class、方法寫註解寫出他們的用處 如果想要關掉的話就加``"--disable=C0114, C0115, C0116",``在剛剛的 [ ] 裡面 (C0114=>檔案註解 C0115=>class註解 C0016=>方法註解) 成功的話有問題會顯示在 Prbblems `` Ctrl + Shift + M `` ![](https://i.imgur.com/tPa5fuf.png) # Django 2.1 ## 環境設定 --- ### Python 安裝 --- [Python 3.6.6](https://www.python.org/ftp/python/3.6.6/python-3.6.6-amd64-webinstall.exe) 要記得 ☑ Add Python 3.6 to PATH ![](https://i.imgur.com/92toTsF.png) 安裝完成之後,打開 VSCode 並叫出 Terminal ( 終端機 ) 輸入 python --version ![](https://i.imgur.com/oaYvnxn.png) ### VSCode 套件安裝 --- 下載 VSCode 的擴充套件 Python 套件 ![](https://i.imgur.com/xkdzMGx.png) Django 套件 ![](https://i.imgur.com/gFSKyiV.png) ### Python 虛擬環境建置 --- 接下來我們要建立虛擬環境目錄 一樣在 VSCode 的 Terminal 上先 cd 到你要創建的地方 輸入 mkdir 資料夾名稱創建好後 cd 進去 ![](https://i.imgur.com/oscpUq9.png) 進到資料夾裡後輸入 python -m venv 虛擬環境名稱 ![](https://i.imgur.com/1IgW7gB.png) 先隨便創建個 .py 檔 接著重啟 VSCode 的 Terminal 然後他就會自動輸入指令讓你進入虛擬環境裡拉 ![](https://i.imgur.com/SbOKpke.png) 想退出虛擬環境的話輸入 deactivate 就行囉 想要重新進入虛擬環境的話可以輸入 虛擬環境名稱\Scripts\activate ![](https://i.imgur.com/LTltpGY.png) 可能有些人在進入虛擬環境時會報錯 這時要用系統管理員的權限去開啟 PowerShell ![](https://i.imgur.com/xrboSKI.png) 開好後輸入 Set-ExecutionPolicy RemoteSigned 再輸入 y ![](https://i.imgur.com/6C28O4E.png) ### Django 2.1 安裝 --- 在虛擬環境裡輸入 pip install "django<2.2" 輸入 django<2.2 可以確保我們安裝的是 2.1 的最新版本 ![](https://i.imgur.com/3WNpKGi.png) #### 確認安裝成功 --- 第一種方法 接著輸入 python 進入進入互動式命令列環境 進入後輸入 ``` python= import django django.VERSION ``` 輸入完後會取得 Django 的版本訊息 ![](https://i.imgur.com/McaOLEm.png) 要退出互動式命令列環境輸入 ``exit()`` 第二種方法 直接在 terminal 上輸入 django-admin --version ![](https://i.imgur.com/XxSPOXD.png) ### 建置 Django 第一個 project --- 在虛擬環境裡輸入 django-admin startproject 專案名稱 ![](https://i.imgur.com/hNdvKXF.png) ![](https://i.imgur.com/TRX2mCl.png) ### VSCode 基本環境設定 --- 接著用 VSCode 開啟新創建得專案資料夾 ![](https://i.imgur.com/HS6NDIc.png) 打開 VSCode 的 settings ( 設定 ) 快捷鍵 ``ctrl + ,`` 在搜尋欄打上 Python 再把選項切換到 Workspace 上 再點選 Edit in settings.json ![](https://i.imgur.com/4ODfIlX.png) 在 settings.json 加上 ```json= "python.defaultInterpreterPath": "虛擬環境路徑\\Scripts\\python.exe", ``` 可以直接找到虛擬環境的目錄去複製 但要記得路徑的 \ 在 Window 的 VSCode 裡要用 \\ 看到下面狀態欄變 ( 虛擬目錄名稱 : venv ) 就成功了 ![](https://i.imgur.com/jMiQvXY.png) ### 建置 Django 第一個 app --- 這邊先說明 Django 的一個 app 就代表網站上的一個功能 所以通常一個網站都會有多個 app 去組成 那要創建一個 app 呢 在虛擬環境裡輸入 python manage.py startapp 功能名稱 ![](https://i.imgur.com/JkJQhUE.png) ![](https://i.imgur.com/j3ijMXw.png) ### Django settings 修改 --- 創建好後打開 settings.py 找到 installed_app 裡面加剛剛我們創建的 app ![](https://i.imgur.com/TMeCczi.png) 修改 107 行以及 109 行改成 zh-Hant ( 中文繁體 ) 以及 'Asia/Taipei' ( 台北時區 ) ![](https://i.imgur.com/DlfRon8.png) ## 簡單實作 --- ### 歡迎介面 找到剛剛創建的 app 資料夾底下的 views.py 寫下下面的程式碼 ``` python= from django.shortcuts import render from django.http import HttpResponse def index(request): return HttpResponse("歡迎來到 Django") ``` 接著換到 urls.py 的檔案寫下下方的程式碼 ``` python= from django.contrib import admin from django.urls import path from news import views urlpatterns = [ path('admin/', admin.site.urls), path('', views.index), ] ``` 最後按下 f5 VSCode debug 運行檢查過沒 bug 就會自動啟動伺服器了 ![](https://i.imgur.com/VkojMVk.png) 最後在網頁上輸入 http://127.0.0.1:8000/ ---

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully