Part 2: 進入後台,增加 NOTE 功能! - amychenmit/misdj002 GitHub Wiki

將依照這個節奏:https://tutorial.djangogirls.org/en/django_start_project/

再開啟另一個 Terminal,啟動虛擬環境,繼續編輯程式:

. venv/bin/activate

bq. #確定到有虛擬資料夾的位置再啟動
./manage.py migrate

migrate:把 model 實現在數據庫中

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

創建後台帳號密碼

./manage.py createsuperuser

#manage.py: 一个让你用各种方式管理 Django 项目的命令行工具。
获取所有 manage.py 的细节: https://docs.djangoproject.com/zh-hans/2.2/ref/django-admin/

###用戶名自己設定
創一個管理員帳號,建好了以後,用剛才的帳密登入:
http://127.0.0.1:8000/admin/login/?next=/admin/

再新增一個用戶叫 user01
底下的『有效』和『人員狀態』要打勾

確定基礎 function 可以運作

@misdj directory

./manage.py startapp note

Creates a Django app directory structure for the given app name in the current directory or the given destination.
By default, the new directory contains a models.py file and other app template files. If only the app name is given, the app directory will be created in the current working directory.
在此資料夾創立了 note 的資料夾,裡面包含 Django 基本的 app 內容。

replace the following codes to misdj/note/models.py

from django.db import models

class Note(models.Model):
subject = models.CharField(max_length=200)

def str(self): return self.subject

add ‘note’ to line 41 at misdj/misdj/settings.py


  1. Application definition

INSTALLED_APPS = [
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,

‘note’, # 加這一行

]

save all files

./manage.py makemigrations
./manage.py migrate

#如果沒存成功會顯示 No changes detected

When you add new apps to INSTALLED_APPS, be sure to run manage.py migrate, optionally making migrations for them first with manage.py makemigrations.

成功:

eanneshen$ ./manage.py makemigrations
Migrations for 'note':
  note/migrations/0001_initial.py
    - Create model Note
(venv) leanneshendeMacBook-Air:misdj leanneshen$ ./manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, note, sessions
Running migrations:
  Applying note.0001_initial... OK

Adding one line at misdj/note/admin


from django.contrib import admin, #加這行
from .models import Note

admin.site.register(Note)

到後台看是否有增加 note 功能
http://127.0.0.1:8000/admin

將本地代碼上傳到 github 的組合拳


git add .
git status
git commit -m"你的註釋"
git status
git push -u origin master
⚠️ **GitHub.com Fallback** ⚠️