CentOS 6 安装 python2.7

CentOS 6 预装的python版本是2.6,由于python2.6不支持ipython等一系列库,所以需要安装python2.7,python3如果有需要安装方法应该类似。

安装python2.7不建议直接覆盖python2.6,因为yum本身是需要Python2.6的支持,有见过直接覆盖后yum就失效的情况。因此使用python2.7我们采用virtualenv的方法,不会影响原生的2.6版本。

安装参考 CENTOS 6.5 安装 Python 2.7 总结,总共分为3部分:

1.安装python2.7

2.安装2.7对应的easy_install 和 pip

3.安装virtualenv

安装过程中会需要用到zlib和openssl,需要在安装python2.7前就安装好。因此步骤大致如下:

预安装:

yum install zlib-devel

yum install openssl-devel

1.

wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
tar xf Python-2.7.8.tgz
cd Python-2.7.8
./configure –prefix=/usr/local
make && make install

2.

# First get the setup script for Setuptools:
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py

# Then install it for Python 2.7 :
python2.7 ez_setup.py

# Now install pip using the newly installed setuptools:
easy_install-2.7 pip

3.

# Install virtualenv for Python 2.7 and create a sandbox called my27project:
pip2.7 install virtualenv
virtualenv-2.7 my27project

# Check the system Python interpreter version:
python –version
# This will show Python 2.6.6

# Activate the my27project sandbox and check the version of the default Python interpreter in it:
source my27project/bin/activate
python –version
# This will show Python 2.7.X

之后每次使用python2.7只需要source一下active文件就可以了

参考:

CENTOS 6.5 安装 Python 2.7 总结

 

django学习第一天

整个流程完全按照django官网的tutorial进行

今天对django的 Writing your first Django app, part 1 和 Writing your first Django app, part 2 进行了学习,边看文档边自己构建一个django项目

学习过程中的关键点:

1.django使用的数据库自带的sqlite3, 这个在目前项目中的UnitTest时也是使用的自带的sqlite3. 在此之外还可以使用 mysql, PostgreSQL等。在settings文件中对DATABASE的描述中,不同的数据库选用不同的ENGINE, 相对应的NAME( sqlite3使用 本地文件路径,默认路径是os.path.join(BASE_DIR,‘db.sqlite3’)), 使用非sqlite3需要填入USER, PASSWORD, HOST信息

2.django在运行前需要先执行 python manage.py migrate对数据库做初始化操作

3.执行 python manage.py runserver 默认端是8000, 如果需要其他ip对服务进行访问,需要执行 python manage.py runserver 0.0.0.0:8000

4.创建新的app,需要执行python manage.py startapp polls, polls 为app名字,然后在settings.py中的INSTALLED_APPS中添加’polls’在最后,例如

mysite/settings.py
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)

5.编写app中的models.py文件,定义app中的数据结构,执行

python manage.py makemigrations polls

根据models的变化生成新的migration文件

python manage.py sqlmigrate polls 0001

查询migrate对应的sql操作语句

python manage.py migrate

对数据库进行操作

Migrations are very powerful and let you change your models over time, as you develop your project, without the need to delete your database or tables and make new ones – it specializes in upgrading your database live, without losing data. We’ll cover them in more depth in a later part of the tutorial, but for now, remember the three-step guide to making model changes:

6.创建超级用户

python manage.py createsuperuser

7.注册新的app到admin中

polls/admin.py
from django.contrib import admin

from .models import Question

admin.site.register(Question)

8.自定义admin中app的格式

polls/admin.py
from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    fields = ['pub_date', 'question_text']

admin.site.register(Question, QuestionAdmin)

9.admin中对两个model间建立关系

polls/admin.py
from django.contrib import admin

from .models import Choice, Question


class ChoiceInline(admin.StackedInline):
    model = Choice
    extra = 3


class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines = [ChoiceInline]

admin.site.register(Question, QuestionAdmin)

10.admin中一些小的展示技巧 list_display,  list_filter,  search_fields, 后续admin专题有更详细的学习