Abstract
继续Python Django 博客开发系列,这篇主要分享环境的搭建和基础配置
Before Installation
- 在按照Django之前,强烈建议大家按照 python 切换pip 豆瓣源 把pip的源换了,实际测试豆瓣源安装15秒完成,官网只能说so slow
- 建议大家参考Centos 下 pyenv 安装 python 多版本 和 virtualenv纯净虚拟环境 配合来新增Python项目安装
- 另外这里提供 自动化安装和配置脚本来 自动化安装pyenv+virtualenv ,记得要
source install_pyenv.sh
来执行哦 - 脚本地址 https://github.com/opscolin/ShellScripts/blob/master/install_pyenv.sh 欢迎大家star
附自动化安装脚本部分截图:
[root@i-pp837f3z ~]# source install_pyenv.sh
default install to ~/.pyenv
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirror.bit.edu.cn
* updates: mirrors.btte.net
Package readline-6.2-9.el7.x86_64 already installed and latest version
Package 1:openssl-1.0.1e-60.el7_3.1.x86_64 already installed and latest version
Package bzip2-libs-1.0.6-13.el7.x86_64 already installed and latest version
... ...
... ...
Dependency Installed:
keyutils-libs-devel.x86_64 0:1.5.8-3.el7 krb5-devel.x86_64 0:1.14.1-27.el7_3 libcom_err-devel.x86_64 0:1.42.9-9.el7 libgnome-keyring.x86_64 0:3.8.0-3.el7 libkadm5.x86_64 0:1.14.1-27.el7_3
libselinux-devel.x86_64 0:2.5-6.el7 libsepol-devel.x86_64 0:2.5-6.el7 libverto-devel.x86_64 0:0.2.5-4.el7 ncurses-devel.x86_64 0:5.9-13.20130511.el7 pcre-devel.x86_64 0:8.32-15.el7_2.1
perl-Error.noarch 1:0.17020-2.el7 perl-Git.noarch 0:1.8.3.1-6.el7_2.1 perl-TermReadKey.x86_64 0:2.30-20.el7 rsync.x86_64 0:3.0.9-17.el7 zlib-devel.x86_64 0:1.2.7-17.el7
Complete!
Cloning into '/root/.pyenv'...
remote: Counting objects: 15365, done.
remote: Compressing objects: 100% (29/29), done.
remote: Total 15365 (delta 50), reused 63 (delta 43), pack-reused 15291
Receiving objects: 100% (15365/15365), 2.74 MiB | 1.32 MiB/s, done.
Resolving deltas: 100% (10511/10511), done.
Cloning into '/root/.pyenv/plugins/pyenv-virtualenv'...
remote: Counting objects: 1901, done.
remote: Total 1901 (delta 0), reused 0 (delta 0), pack-reused 1901
Receiving objects: 100% (1901/1901), 537.87 KiB | 255.00 KiB/s, done.
Resolving deltas: 100% (1305/1305), done.
安装完成验证
[root@i-pp837f3z ~]# pyenv versions
* system (set by /root/.pyenv/version)
Install Django
这里安装超级快,因为我们切换了 pip
的 安装源
[root@i-pp837f3z ~]# pip install django==1.11.4
Collecting django==1.11.4
Downloading https://pypi.doubanio.com/packages/fc/fb/01e0084061c50f1160c2db5565ff1c3d8d76f2a76f67cd282835ee64e04a/Django-1.11.4-py2.py3-none-any.whl (6.9MB)
100% |████████████████████████████████| 7.0MB 6.4MB/s
Collecting pytz (from django==1.11.4)
Downloading https://pypi.doubanio.com/packages/55/62/e7cd0e15b76062d298413f14bb4ec3cd8568a22d274427f9c3c7286969f4/pytz-2017.2-py2.py3-none-any.whl (484kB)
100% |████████████████████████████████| 491kB 3.9MB/s
Installing collected packages: pytz, django
Successfully installed django-1.11.4 pytz-2017.2
[root@i-pp837f3z ~]#
Verify Installation
[root@i-pp837f3z ~]# python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.__version__
'1.11.4'
>>>
Django 项目初始
创建项目
进入到你的工程目录下面去,然后开始我们的Django之旅
root@i-pp837f3z opt]# cd /opt/djangoproject/
[root@i-pp837f3z djangoproject]# pwd
/opt/djangoproject
[root@i-pp837f3z djangoproject]# django-admin.py startproject myblog
[root@i-pp837f3z djangoproject]# tree myblog/
myblog/
|-- manage.py
`-- myblog
|-- __init__.py
|-- settings.py
|-- urls.py
`-- wsgi.py
1 directory, 5 files
django-admin.py VS manage.py
-
django-admin.py
是Django项目的入口命令行管理工具集,通过他可以创建我们的项目、同步数据库、等等操作, 你可以
django-admin.py --help
查看全部用法 -
manage.py
和django-admin.py有相同的用处,不过额外的还有别的和项目特殊相关的。在创建每个项目的时候都会自动生成一个
manage.py
文件,它会把你的项目自动加入到 sys.path中去等,具体可以参考:https://docs.djangoproject.com/en/1.11/ref/django-admin/
创建应用
[root@i-pp837f3z myblog]# python manage.py startapp blog
[root@i-pp837f3z myblog]# tree .
.
|-- blog
| |-- admin.py
| |-- apps.py
| |-- __init__.py
| |-- migrations
| | `-- __init__.py
| |-- models.py
| |-- tests.py
| `-- views.py
|-- manage.py
`-- myblog
|-- __init__.py
|-- __init__.pyc
|-- settings.py
|-- settings.pyc
|-- urls.py
`-- wsgi.py
配置应用
vim myblog/settings.py
把新增APP添加到INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
]
vim myblog/urls.py
新增关于 blog 应用的URL解析
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^blog/', include('blog.urls')),
url(r'^admin/', admin.site.urls),
]
这里现在blog应用中新增一个index view
vim blog/views.py
# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse('hello django blog')
然后在 blog 目录下新增 urls.py 如下
vim blog/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
在上面这些初始化配置完成之后,我们只可以启动服务看看效果
启动服务
python manage.py runserver 0.0.0.0:8080
这里的 0.0.0.0
代表任何主机可访问, 8002
是对外提供服务的端口
然后我们在浏览器中输入 192.168.88.2:8080
查看效果
出现这个需要们在 myblog/settings.py 中修改配置
ALLOWED_HOSTS = []
为 ALLOWED_HOSTS = [*]
或者 ALLOWED_HOSTS = ['192.168.88.2']
再次刷新显示:
出现 hello django blog
证明我们基本环境已经部署oK ...