- 安装
Ubuntu:
sudo apt install python-pip
sudo pip install django
sudo pip install Django==1.8.16(指定 Django 版本)
CentOS:
yum -y install python-pip
pip install django
- Django 基础命令
django-adimn startproject test_pro # 创建一个项目名为 test_pro 的项目
cd test_pro
python manage.py startapp test_app # 在 test_pro 项目中创建一个 app
python manage.py runserver 0.0.0.0:8000 # 开启测试端口
(修改setting.py的allow_hosts字段允许监听的IP地址)
3.Apache 部署 Django 项目
- 系统环境
操作系统:Ubuntu 16.04
Apache版本:2.4.18
项目目录:/web/mysite
- 安装 Apache 和 mod_wsgi
sudo apt-get install apache2 libapache2-mod-wsgi
- 配置虚拟主机
vi /etc/apache2/sites-enabled/sitename.conf
###################################
<VirtualHost *:80>
ServerName www.imlcs.top
Alias /media/ /web/mysite/media/ # 用户上传文件目录
Alias /static/ /web/mysite/static/ #网站静态文件目录
<Directory /web/mysite/media>
Require all granted
</Directory>
<Directory /web/mysite/static>
Require all granted
</Directory>
WSGIScriptAlias / /web/mysite/mysite/wsgi.py
<Directory /web/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
- 修改 wsgi.py
import os
import sys
from os.path import dirname,abspath
PROJECT_DIR = dirname(dirname(abspath(__file__)))
sys.path.insert(0,PROJECT_DIR)
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
- 修改 settings.py 添加以下内容
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/dev/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
# upload folder
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
- 测试结果