一.基本的开发环境
1.1 要求
Python 2.7.x
easy_install and pip
Git 1.7/1.8
virtualenv
Django 1.6.0
Database (SQLite, MySQL, PostgreSQL)
South
编辑器 Sublime, vim, Komodo, gedit)
1.2.MySQL启动
安装见官网http://dev.mysql.com/doc/refman/5.7/en/installing.html
$ mysql.server start
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
mysql> CREATE DATABASE django_db;
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> quit
Bye
1.3.安装virtualenv
virtualenv相当于沙盒
pip install virtualenv
mkdir new_project
cd new_project
virtualenv --no-site-packages env
. env/bin/activate
1.4.安装Django
pip install django==1.6.0
查看django的版本
python
>>> import django
>>> django.get_version()
'1.5.4'
二.建立工程
django-admin.py startproject mysite
结构如图
├── manage.py
└── mysite
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
使用git
git init
git add .
git commit -m "initial commit"
数据库设置
apt-get install python-setuptools
apt-get install libmysqld-dev
apt-get install libmysqlclient-dev
apt-get install python-dev
pip install MySQL-python
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'USER': 'root',
'PASSWORD': 'your_password',
}
}
创建数据表和管理员
$ cd mysite
$ python manage.py syncdb
Creating table auth_message
Creating table auth_group
Creating table auth_user
Creating table auth_permission
Creating table django_content_type
Creating table django_session
Creating table django_site
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username: admin
E-mail address: your@email.com
Password:
Password (again):
Superuser created successfully.
Installing index for auth.Message model
Installing index for auth.Permission model
Loading 'initial_data' fixtures...
No fixtures found.
South
用来动态地改变models.py
pip install south
Pip freeze
对已安装的libraries 作一个记录
pip freeze > requirements.txt
创建App
python manage.py startapp myapp
项目结构如图
├── manage.py
├── myapp
│ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── mysite
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
修改settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'myapp',
'south',
)
urls.py
添加管理界面
from django.contrib import admin
admin.autodiscover()
url(r'^admin/', include(admin.site.urls)),
同步数据库
python manage.py syncdb
使用South
python manage.py schemamigration myapp --initial
合并数据库改变
python manage.py migrate myapp
Git使用
如果使用csdn code
vi ~/.gitconfig
[user]
name = wcc526
email = 949409306@qq.com
[core]
editor=vi
[alias]
ci=commit -a -v
throw=reset --hard HEAD
throwh=reset --hard HEAD^
vi ~/.bashrc
alias g='git'
# Autocomplete for 'g' as well
complete -o default -o nospace -F _git g
cd ~/.ssh
ssh-keygen -t rsa -C “haiyan.xu.vip@gmail.com”
git config --global user.name "defnngj"//给自己起个用户名
git config --global user.email "defnngj@gmail.com"//填写自己的邮箱
git remote add origin git@github.com:luozhaoyu/test.git
git add .
git commit -m "updated settings, created app, added south, enabled django admin"
git clone /path/to/your/project/
如/Users/michaelherman/desktop/new_project
git branch <branchname>
#转变分支
git checkout <branchname>
#查看分支
git branch
#如果完成开发,准备提交
git add .
git commit -a
#如果你需要合并改变到主分支
git checkout master
git merge <branchname>
#推送到github
git push
#回到主文件夹,PULL所有的改变
git pull
例子
(env)$ cd dev
(env)$ cd new_project
(env)$ git branch 06212013
(env)$ git checkout 06212013
(env)$ git add .
(env)$ git commit -m "description of changes made"
(env)$ git checkout master
(env)$ git merge 06212013
(env)$ git pull /Users/michaelherman/desktop/new_project/dev/new_project
作为代码管理工作,我们随时可以 "反悔"。
使用 "git reset HEAD <filename>" 命令可以取消暂存区的文件快照(即恢复成最后⼀一个提交版
本),这不会影响工作目录的文件修改。
使用 "git checkout -- <filename>" 从仓库恢复工作目录文件,暂存区不受影响。
$ git chekcout -- readme
在 Git 中 "HEAD" 表⽰示仓库中最后⼀一个提交版本,"HEAD^" 是倒数第二个版本,"HEAD~2" 则是更
老的版本。
我们可以直接 "签出" 代码仓库中的某个文件版本到工作目录,该操作同时会取消暂存区快照。
$ git checkout HEAD^ readme
如果想将整个项目回溯到以前的某个版本,可以使用 "git reset"。可以选择的参数包括默认的 "--
mixed" 和 "--hard",前者不会取消工作目录的修改,而后者则放弃全部的修改。该操作会丢失其
后的⽇日志。
$ git reset --hard HEAD^
有时,你需要开始一个新分支,但是又不想把很长很长的历史记录带进来,例如,你想在公众区域(开源)放置你的代码,但是又不想别人知道它的历史记录。
git checkout --orphan NEW_BRANCH_NAME_HERE
分支重命名
git branch -m <newname>
git branch -m <oldname> <newname>
工作流
1.Activate virtualenv
2.Create and checkout a new git branch
3.Develop
4.Commit changes
5.Merge the new branch with your master branch
6.PULL the changes into the production folder
7.Deploy
创建基本App
使用markdown 展示文本
pip install django-markdown-deux
pip freeze > requirements.txt
settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'myapp',
'south',
'markdown_deux',
)
TEMPLATE_DIRS = (
'/Users/michaelherman/desktop/new_project/mysite/templates'
)
项目结构如图
├── manage.py
├── myapp
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── mysite
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
└── templates
views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
def index(request):
return render_to_response('index.html')
urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'myapp.views.index')
)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Change me</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="//code.jquery.com/jquery-1.10.1.min.js" type="text/javascript"></script>
</head>
<body>
<div class="container">
{% load markdown_deux_tags %}
{% markdown %}
{% markdown_cheatsheet %}
{% endmarkdown %}
</div> <!-- /container -->
</body>
</html>