Wei-Ting
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • 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

      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.
      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
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • 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 Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • 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

    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.
    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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: python --- # DAY 26 Django允許使用者輸入資料 我們要讓使用者可以新增主題開始,建置以表單為基礎的頁面,其方法與前面建置網頁幾乎一樣,要先定義URL,編寫視圖函式和編寫模板,其中不一樣的地方是要匯入含有表單模組的forms.py檔 ## 新增主題 ### 主題的ModelForm 任何能讓使用者輸入並提交資訊的頁面都是表單,在Django中建置表單最簡單的方式就是使用ModelForm,它會依據一開始定義的模型中的資訊自動建立表單 :arrow_down: 建立一個forms.py檔存放在models.py所在的資料夾,如下 ```python= from django import forms from .models import Topic class TopicForm(forms.ModelForm): class Meta: model = Topic # 依據Topic模型來建立表單 fields = ['text'] # 表單只有text欄位 labels = {'text': ''} # 不要在text欄位加標籤 ``` ### new_topic URL模式 :arrow_down: 將new_topic頁面的URL模式新增到learning_logs資料夾中的urls.py中,如下 ```python= --省略-- urlpatterns=[ --省略-- path('new_topic/',views.new_topic,name='new_topic'), ] ``` ### new_topic()視圖函式 new_topic()函式需要處理兩種情況,第一種情況是剛進入new_topic網頁這種情況會顯示空表單,第二種情況是提交表單對資料進行處理,並將使用者重新導向到topics網頁中 :arrow_down: 新增一些內容至views.py檔 ```python= from django.shortcuts import render from django.http import HttpResponseRedirect # 新增區塊 from django.urls import reverse # 新增區塊 from .models import Topic from .forms import TopicForm # 新增區塊 --省略-- # 以下為新增區塊 def new_topic(request): if request.method != 'POST': form = TopicForm() else: form = TopicForm(request.POST) if form.is_valid(): new_topic.save() return HttpResponseRedirect(reverse('learning_logs:topics')) context = {'form': form} return render(request, 'learning_logs/new_topic.html', context) ``` 在上面程式碼第二行匯入HttpResponseRedirect類別會讓使用者提交主題後重新導向至topics網頁,第十二行是要確定請求方法是GET還是POST,如果不是POST那就會是玵後返回一個空表單,第十五行建立一個TopicForm實例,把它存到form變數內再將它存到第二十行的context字典,第十六行是確認使用者有沒有對所有欄位填寫資料,如果有就可呼叫第十七行的save(),儲存資料後我們就可以離開頁面,使用reverse()來取得頁面topics的URL,並將它傳送到HttpResponseRedirect(),然後將使用者重新導向topics頁面 ### new_topic模板 :arrow_down: 建立一個新的new_topic.html模板存放在index.html同個資料夾中 ```html= {% extends "learning_logs/base.html" %} {% block content %} <p>Add a new topic:</p> <form action="{% url 'learning_logs:new_topic' %}" method='post'> {% csrf_token %} {{ form.as_p }} <button name="submit">add topic</button> </form> {% endblock content %} ``` 上面程式碼第六行是告訴伺服器要提交的表單資料要傳送到哪裡,第七行是防止駭客用表單來取得對伺服器未經授權的存取,第八行是顯示表單,第九行是為了提交表單建立一個按鈕 ### 連結到new_topic頁面 :arrow_down: 開啟topics.html新增一個連到new_topic的連結 ```html= {% extends "learning_logs/base.html" %} {% block content %} <p>Topics</p> --省略-- <a href="{% url 'learning_logs:new_topic' %}">Add new topic</a> {% endblock content %} ``` :arrow_down:用來新增主題的頁面如下 **先按Add new topic** ![](https://i.imgur.com/G05eCQI.png) **進入新增頁面** ![](https://i.imgur.com/BY4w30W.png) --- ## 新增紀錄項目 ### 紀錄項目的ModelForm 建立一個與Entry模型相關聯的表單 :arrow_down: 新增一些程式碼至forms.py檔 ```python= from django import forms from .models import Topic, Entry # 新增區塊 class TopicForm(forms.ModelForm): --省略-- # 以下為新增區塊 class EntryForm(forms.ModelForm): class Meta: model = Entry fields = ['text'] labels = {'text': ''} widgets = {'text': forms.Textarea(attrs={'cols': 80})} # 定義一個文字區塊寬度為80欄 ``` ### new_entry URL模式 用來新增紀錄項目的頁面URL模式中需要放入topic_id引數,因為紀錄需要和主題相關聯 :arrow_down: 將new_entry頁面的URL模式新增到learning_logs資料夾中的urls.py中,如下 ```python= --省略-- urlpatterns=[ --省略-- path('new_entry/<int:topic_id>/',views.new_entry,name='new_entry'), ] ``` ### new_entry()視圖函式 :arrow_down: 新增一些內容至views.py檔 ```python= --省略-- from .forms import TopicForm,EntryForm # 新增區塊 --省略-- # 以下為新增區塊 def new_entry(request, topic_id): topic = Topic.objects.get(id=topic_id) # 使用topic_id來取得正確主題物件 if request.method != 'POST': form = EntryForm() else: form = EntryForm(data=request.POST) if form.is_valid(): new_entry = form.save(commit=False) new_entry.topic = topic new_entry.save() return HttpResponseRedirect(reverse('learning_logs:topic',args=[topic_id])) context = {'topic': topic, 'form': form} return render(request, 'learning_logs/new_entry.html', context) ``` ### new_entry模板 :arrow_down: 建立一個新的new_entry.html模板存放在index.html同個資料夾中 ```html= {% extends "learning_logs/base.html" %} {% block content %} <p><a href="{% url 'learning_logs:topic' topic.id %}">{{ topic }}</a></p> <p>Add a new entry:</p> <form action="{% url 'learning_logs:new_entry' topic.id %}" method='post'> {% csrf_token %} {{ form.as_p }} <button name='submit'>add entry</button> </form> {% endblock content %} ``` 上面程式碼第三行是讓使用者知道是在哪一個主題中新增項目,第八行可讓視圖函式能把新項目關連到正確的主題 ### 連結到new_entry頁面 :arrow_down: 開啟topic.html新增一個連到new_entry的連結 ```html= {% extends 'learning_logs/base.html' %} {% block content %} <p>Topic: {{ topic }}</p> <p>Entries:</p> <p> <a href="{% url 'learning_logs:new_entry' topic.id %}">add new entry</a> </p> <ul> --省略-- </ul> {% endblock content %} ``` :arrow_down:用來新增紀錄項目的頁面如下 **先按add new entry** ![](https://i.imgur.com/e4eDIEE.png) **進入新增頁面** ![](https://i.imgur.com/jSBRVL3.png) ## 編輯紀錄項目 建立一個頁面可讓使用者編輯現有的紀錄項目 ### edit_entry URL模式 這個頁面的URL需要傳入想編輯的紀錄項目ID模式 :arrow_down: 開啟learning_logs資料夾中的urls.py中,如下 ```python= --省略-- urlpatterns=[ --省略-- path('edit_entry/<int:entry_id>/', views.edit_entry, name='edit_entry'), ] ``` ### edit_entry()視圖函式 :arrow_down: 新增一些內容至views.py檔 ```python= --省略-- from .models import Topic,Entry # 新增區塊 --省略-- # 以下為新增區塊 def edit_entry(request, entry_id): """Edit an existing entry.""" entry = Entry.objects.get(id=entry_id) topic = entry.topic if request.method != 'POST': form = EntryForm(instance=entry) else: form = EntryForm(instance=entry, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('learning_logs:topic',args=[topic.id])) context = {'entry': entry, 'topic': topic, 'form': form} return render(request, 'learning_logs/edit_entry.html',context) ``` ### edit_entry模板 :arrow_down: 建立一個新的edit_entry.html模板存放在index.html同個資料夾中 ```html= {% extends "learning_logs/base.html" %} {% block content %} <p><a href="{% url 'learning_logs:topic' topic.id %}">{{ topic }}</a></p> <p>Edit entry:</p> <form action="{% url 'learning_logs:edit_entry' entry.id %}" method='post'> {% csrf_token %} {{ form.as_p }} <button name="submit">save changes</button> </form> {% endblock content %} ``` 上面程式碼第九行把紀錄項目的id當成引數放在{%url%}標記,讓視圖物件能修改到正確的紀錄項目物件,第十二行是提醒使用者要按按鈕儲存修改的內容 ### 連結到edut_entry頁面 :arrow_down: 開啟topic.html新增一個連到edit_entry的連結 ```html= --省略-- {% for entry in entries %} <li> <p>{{ entry.date_added|date:'M d, Y H:i' }}</p> <p>{{ entry.text|linebreaks }}</p> <p> <a href="{% url 'learning_logs:edit_entry' entry.id %}">edit entry</a> </p> </li> {% empty %} --省略-- {% endblock content %} ``` :arrow_down:用來編輯紀錄項目的頁面如下 每個紀錄項目後面都有一個編輯連結 ![](https://i.imgur.com/3VxZwMD.png) #### 今天結束,各位明天見 :hand: *** 資料來源:<<python程式設計的樂趣>>-Eric Matthes著/H&C譯

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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