1 Mac系統默認自帶Python,查看版本:
命令行輸入:python
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
版本號爲2.7.6
所以不用安裝了
2 安裝Django,最新的版本1.7
(1)首先使用easy_install安裝pip(easy_install是Python的包管理工具,類似Ruby下的gem,pip是升級版的easy_install), sudo easy_install pip
(2)安裝成功後,安裝Django, pip install Django==1.7
(3)查看安裝路徑:默認爲/usr/bin,如果看到django-admin.py說明安裝成功,django-admin.py是Django的管理工具,用來生成項目和應用
3 連接mysql
(1)安裝mysql for mac,直接從官網上下載dmg文件安裝即可,需要64位版本
(2)安裝mysql python驅動, sudo easy_install mysql-python
4 創建第一個項目
(1)創建項目:django-admin.py startproject demoproject
(2)創建應用:
cd demoproject
python manage.py startapp demoapp
創建成功
(3)修改settting.py,將demoapp加入到INSTALLED_APPS
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'demoapp'
)
(4)修改settting.py,將默認的sqlite數據庫換成mysql
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djangodb',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
(5)在demoproject下輸入:python manage.py dbshell,如果能正常進入mysql命令行,則說明連接成功
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 270
Server version: 5.5.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
5 啓動應用
(1)同步數據庫:執行python manage.py syncdb,第一次啓動需要創建superuser,用來管理django後臺
Operations to perform:
Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying sessions.0001_initial... OK
You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'thierry'): thierry
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.
(2)啓動服務:python manage.py runserver:
Performing system checks...
System check identified no issues (0 silenced).
December 03, 2014 - 08:36:46
Django version 1.7, using settings 'djproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
啓動成功,在瀏覽器輸入http://127.0.0.1:8000/打開應用
在瀏覽器輸入http://127.0.0.1:8000/admin進入後臺管理應用