Django开发环境搭建
2018-01-22 16:20
423 查看
1.虚拟环境
tianshl:workspace tianshl$ mkdir server tianshl:workspace tianshl$ cd server/ tianshl:server tianshl$ virtualenv venv --python=python3 tianshl:server tianshl$ source venv/bin/activate
2. 安装依赖
(venv) tianshl:server tianshl$ pip install django (venv) tianshl:server tianshl$ pip install djangorestframework
3. 创建项目
(venv) tianshl:server tianshl$ django-admin.py startproject server (venv) tianshl:server tianshl$ tree server/ server ├── manage.py └── server ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
4. 更新配置
(venv) tianshl:server tianshl$ cd server/ (venv) tianshl:server tianshl$ vim server/settings.py # 1.修改 INSTALLED_APPS = ( ... 'rest_framework', ) # 2.添加 REST_FRAMEWORK = { # Use Django's standard `django.contrib.auth` permissions, # or allow read-only access for unauthenticated users. 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' ] } # 3.修改 ALLOWED_HOSTS=['*']
5. 同步数据表
(venv) tianshl:server tianshl$ python manage.py migrate
6. 创建管理员
(venv) tianshl:server tianshl$ python manage.py createsuperuser
7. 启动项目
(venv) tianshl:server tianshl$ python manage.py runserver 0.0.0.0:80
8. 测试
http://localhost http://localhost/admin[/code]附加
1. 无法创建虚拟环境问题: pip._vendor.requests.exceptions.HTTPError: 404 Client Error: Not Found for url: http://pypi.doubanio.com/simple/pkg-resources/ 原因: 国内安装源的问题(阿里云安装源或其他) 解决: 修改安装源 vim ~/.pip/pip.conf 内容如下: [global] index-url = https://pypi.python.org/simple/ [install] trusted-host=pypi.python.org
2. 启动服务后无法访问问题: 启动Django后,无法访问, 报400错误 原因: 没有开启允许访问 解决: 修改settings.py 中的 ALLOWED_HOSTS=['*']
3. 如何创建App, 添加配置, 同步数据表1. (venv) tianshl:server tianshl$ python manage.py startapp myapp 2. INSTALLED_APPS中添加myapp 3. (venv) tianshl:server tianshl$ python manage.py makemigrations myapp (venv) tianshl:server tianshl$ python manage.py migrate
4. Mac系统运行时提示端口没有权限问题: Error: You don't have permission to access that port. 原因: 低于1024的端口需要管理员权限. 解决: 修改端口大于1024,如8888 或 使用sudo命令: sudo python manage.py runserver 0.0.0.0:80
相关文章推荐
- 在ubuntu下搭建python开发环境(pycharm,postgresql,virtualenv, Django)
- 使用pycharm手动搭建python语言django开发环境(一)
- Django安装-开发环境搭建
- Ubuntu下Django初体验(一)——开发环境搭建
- PyCharm开源社区版+Django搭建web开发环境
- centos 6.4x64 django开发环境搭建(升级python2.6.6为2.7.3)
- python +Django 搭建web开发环境初步,显示当前时间
- Django安装与开发虚拟环境搭建01
- Eclipse+PyDev+Django+Mysql搭建Python web开发环境
- Python使用django搭建web开发环境
- 10、django开发环境搭建及使用
- Django学习笔记 开发环境搭建
- pycharm下搭建django开发环境
- Python之Django商城项目实战(一):搭建开发环境
- 在Ubuntu环境搭建python+Django开发环境
- django 笔记 搭建多个不同版本的开发环境
- python3.6+django2.0+pycharm搭建Web开发环境
- python+Django+pycharm 开发环境搭建
- python + django + pydev +eclipse 搭建网站开发环境
- Django1.7.2+Bootstrap3.3.0 整合一:Python 3.4.2 开发环境搭建