您的位置:首页 > 编程语言 > Go语言

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: