需求描述:
資料表已建好,能視覺化操作增刪改查,避免直接運算元據庫
簡版的管理系統
pip install django
pip install mysqlclient
django-admin startproject myproject
cd myproject
#settings.py
databases =
}
python manage.py createsuperuser
python manage.py runserver #預設8000
#python manage.py runserver 9999
#settings.py
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
from django.contrib import admin
# register your models here.
admin.site.register(post)
pip install django_mysql
#引入django_mysql模組
from django_mysql.models import bit1booleanfield
將models.textfield(blank=true, null=true) # this field type is a guess.
或models.textfield() # this field type is a guess.
批量替換為 bit1booleanfield(default=false)
# settings.py
# security warning: don't run with debug turned on in production!
debug = false
# must set settings.allowed_hosts if debug is false
allowed_hosts = ['localhost','127.0.0.1','example.com']
# settings.py
static_url = '/static/'
static_root = os.path.join(base_dir, 'static') #新增配置
# urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
]+ static(settings.static_url, document_root=settings.static_root)
python manage.py collectstatic
# settings.py
# 語言
#language_code = 'zh-cn'
language_code = 'zh-hans'
# 時區
time_zone = 'asia/shanghai'
use_tz = true
#models.py
class post(models.model):
title = models.charfield('標題', max_length=100)
memo = models.textfield('備註')
publish_date = models.datetimefield('發表時間')
class meta:
verbose_name = '文章'
verbose_name_plural = '文章'
ordering = ['publish_date']
def __unicode__(self):
return self.title
#同步資料庫(可將model改動同步到資料庫)
python manage.py makemigrations
python manage.py migrate
#清空資料庫
python manage.py flush
# 修改 使用者密碼可以用:
python manage.py changepassword username
#匯出資料 匯入資料
#django 專案環境終端
python manage.py shell
#資料庫命令列
python manage.py dbshell
Django 快速搭建簡單的Blog
一,建立專案 我這裡是基於python虛擬環境下做的開發,先進入虛擬環境 workon py3 django 1,為blog建立名為mysite的工程專案 django admin.py startproject mysite cd到專案資料夾 cd mysite 3.建立mysql資料庫 mysq...
Django 快速搭建簡單的Blog
一,建立專案 1,為blog建立名為mysite的工程專案 django admin.py startproject mysite2,專案結構如下 mysite manage.py mysite init py settings.py urls.py wsgi.py 二 執行開發伺服器 要馬上檢視d...
快速搭建django專案
將 orm 模型對映到資料庫中,總結起來就是以下幾步 在 settings.py 中,配置好 databases 做好資料庫相關的配置。在命令列終端,進入到專案所在的路徑,然後執行命令 python manage.py makemigrations 來生成遷移指令碼檔案。同樣在命令列中,執行命令 p...