1、新建乙個mysite專案:django-admin startproject mysite
3、安裝mysqlclient :pip install mysqlclient
4、在settings.py database中設定資料庫連線配置
databases =5、執行命令: python manage.py migrate}
在資料庫中自動建立web系統使用到的表
6、編輯polls/models.py檔案內容
from django.db import'django.contrib.admin',models
class
question(models.model):
question_text = models.charfield(max_length=200)
pub_date = models.datetimefield('
date published')
class
choice(models.model):
question = models.foreignkey(question, on_delete=models.cascade)
choice_text = models.charfield(max_length=200)
votes = models.integerfield(default=0)
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
7、執行命令:python manage.py makemigrations polls
migrations for 'polls':9 再執行python manage.py migrate 命令,建立資料庫表polls/migrations/0001_initial.py:
- create model choice
- create model question
- add field question to choice
8、執行命令:python manage.py sqlmigrate polls 0001
Django 連線 Mysql 資料庫
django專案要運算元據庫,首先要和資料庫建立連線,才能讓程式中的資料和資料庫關聯起來進行資料的增刪改查操作 django專案預設使用mysqldb模組進行和mysql資料庫之間的互動操作,但是mysqldb模組對於python3.4以上的版本支援還不夠完善,所以我們要使用替代方案通過pymysq...
Django連線MySQL資料庫
最近幾天在學習django框架,本以為一天內基本可以擼一遍,沒想到卻花了我兩天的時間。中途踩過一些坑,最大的坑應該是django的orm機制,今晚有空來分享一下。1.如果沒有安裝django,用pip安裝 安裝指定版本 pip3 install django 1.11.5 安裝最新版本 pip3 i...
Django連線mysql資料庫
作業系統 mac os 在專案setting檔案中修改資料庫配置 databases 只是設定這樣的話,執行報錯nameerror name mysql is not defined 安裝mysqlclient 和pymysql pip install mysqlclient pip install...