一.env
因为virtualenv testvir
testvir/Scripts/activate.bat 进入; deactivate 退出
因为这个需要记住目录,接下来介绍pip install virtualenvwrapper-win
mkvirtualenv testvir2
后续进入退出,只需要workon/workon testvir2/deactivate
二.新建工程
新建Django工程
startapp users
并加入到apps目录,目标目录结构如下:
对apps目录右键,mark成为sorce root;并在mooc/settings,把apps加入根目录
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
('css', os.path.join(STATIC_ROOT, 'css')),
('images', os.path.join(STATIC_ROOT, 'images')),
('img', os.path.join(STATIC_ROOT, 'img')),
('js', os.path.join(STATIC_ROOT, 'js')),
]
非orm操作数据库
db = MySQLdb.connect(user='me',db='mydb', passwd='secret', host='127.0.0.1')
cursor = db.cursor()
cursor.execute('SELECT name FROM books ORDER BY name')
names = [row[0] for row in cursor.fetchall()]
db.close()
建立数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': "mooc",
'USER':'root',
'PASSWORD':'root',
'HOST':"127.0.0.1",
'PORT':'3306'
}
}
通过分析页面得出需要model的属性,指定user模块
AUTH_USER_MODEL = "users.UserProfile"
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
('css', os.path.join(STATIC_ROOT, 'css')),
('images', os.path.join(STATIC_ROOT, 'images')),
('img', os.path.join(STATIC_ROOT, 'img')),
('js', os.path.join(STATIC_ROOT, 'js')),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
__init__.py引入
import pymysql
pymysql.install_as_MySQLdb()