安装前准备
检查pip版本与python版本是否一致
[root@localhost bin]# whereis pip
pip: /usr/bin/pip /usr/bin/pip2.7
如果版本不一致,修改pip引用的版本
[root@localhost bin]# mv /usr/bin/pip /usr/bin/pip2.7.2
[root@localhost bin]# ln -s /usr/local/python3.5.2/bin/pip3.5 /usr/bin/pip
安装virtualenv
[root@localhost bin]# pip install virtualenv
Collecting virtualenv
Downloading virtualenv-15.0.2-py2.py3-none-any.whl (1.8MB)
100% |████████████████████████████████| 1.8MB 465kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.0.2
You are using pip version 8.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
创建运行环境
[root@localhost bin]# /usr/local/python3.5.2/bin/virtualenv ~/pythonproject/virtualenv/blog --no-site-packages
Using base prefix '/usr/local/python3.5.2'
New python executable in /root/pythonproject/virtualenv/blog/bin/python3.5
Also creating executable in /root/pythonproject/virtualenv/blog/bin/python
Installing setuptools, pip, wheel...done.
启动virtualenv
source activate
安装django
安装django
[root@localhost bin]# pip install django
Collecting django
Downloading Django-1.9.8-py2.py3-none-any.whl (6.6MB)
100% |████████████████████████████████| 6.6MB 70kB/s
Installing collected packages: django
Successfully installed django-1.9.8
创建django应用
(blog) [root@localhost blog]# cd ~
(blog) [root@localhost ~]# cd blog
-bash: cd: blog: No such file or directory
(blog) [root@localhost ~]# mkdir django
(blog) [root@localhost ~]# cd django/
(blog) [root@localhost django]# django-admin startproject blog
(blog) [root@localhost django]# cd blog/
(blog) [root@localhost blog]# tree
.
├── blog
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-35.pyc
│ │ └── settings.cpython-35.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
2 directories, 7 files
运行django应用
(blog) [root@localhost blog]# python manage.py runserver
报这样一个异常
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f45d54ba488>
Traceback (most recent call last):
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 34, in <module>
from pysqlite2 import dbapi2 as Database
ImportError: No module named 'pysqlite2'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 36, in <module>
from sqlite3 import dbapi2 as Database
File "/usr/local/python3.5.2/lib/python3.5/sqlite3/__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "/usr/local/python3.5.2/lib/python3.5/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named '_sqlite3'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/root/pythonproject/virtualenv/blog/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/models/base.py", line 108, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/models/base.py", line 307, in add_to_class
value.contribute_to_class(cls, name)
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/models/options.py", line 263, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/utils.py", line 212, in __getitem__
backend = load_backend(db['ENGINE'])
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/utils.py", line 116, in load_backend
return import_module('%s.base' % backend_name)
File "/root/pythonproject/virtualenv/blog/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 39, in <module>
raise ImproperlyConfigured("Error loading either pysqlite2 or sqlite3 modules (tried in that order): %s" % exc)
django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named '_sqlite3'
这是由于没有安装sqlite-devel所致。
安装sqlite-devel: yum -y install sqlite-devel
cd ~/Python-3.5.2
编译: ./configure --prefix=/usr/local/python3.5.2
安装: make && make install
再次运行
(blog) [root@localhost blog]# python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
July 27, 2016 - 13:47:44
Django version 1.9.8, using settings 'blog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
此时通过127.0.0.1可以访问,如果想非本机访问,可以在启动时添加相应的参数。如:
python manage.py runserver 0.0.0.0:8000
此时通过http://ip:8000能正常访问了。
django配置
数据库初始化
python manage.py migrate
创建超级用户,用于登录后台
(blog) [root@localhost blog]# python manage.py createsuperuser
Username (leave blank to use 'root'): root
Email address: test@test.com
Password:
Password (again):
Superuser created successfully.
此处账号为root,密码为:root1234。
此时可以通过,http://ip:8000/admin/ 访问后台。输入刚刚创建的用户名和密码即可成功登陆。
![709606-20160727152547231-2115137113.png](https://i-blog.csdnimg.cn/blog_migrate/b53c668c8061bfc23f92c952822fe450.png)
本文转自秋楓博客园博客,原文链接:http://www.cnblogs.com/rwxwsblog/p/5711344.html,如需转载请自行联系原作者