Django是基于Python的开源代码的Web应用框架。采用了MTV的框架模式,即模型M,视图V和模版T。它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的,即是CMS(内容管理系统)软件。
Ubuntu 版本: 18.04
Django 版本:3.0(https://www.djangoproject.com/)
一. 搭建环境
1. 安装 MySQL 或 MariaDB
1) MySQL (5.x.x)
$ sudo apt-get update
$ sudo apt-get install mysql-server mysql-client
$ sudo apt-get install libmysqlclient-dev libssl-dev libcrypto++-dev
Change mysql root password:
$ mysql -u root
(1) use mysql;
(2) update mysql.user set authentication_string=password('123456') where user='root' and Host ='localhost';
(3) update user set plugin="mysql_native_password";
(4) flush privileges;
(5) quit;
2) MariaDB
$ sudo apt-get update
$ sudo apt-get install mariadb-server
2. 安装 Apache2
$ sudo apt-get install apache2
// Ubuntu 下启用 rewrite 模块
$ sudo a2enmod rewrite
$ sudo vim /etc/apache2/sites-enabled/000-default.conf
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
3. 安装 php7 & phpMyAdmin
$ sudo apt-get install php7.x libapache2-mod-php7.x
Create file /var/www/html/index.php, add
<?php
phpinfo()
?>
$ sudo service apache2 restart
Visit http://localhost/index.php, it works!
$ sudo apt-cache search php7.x // Add mysql module for php7.x
$ sudo service apache2 restart
Visit http://localhost/index.php,See mysql module
$ sudo apt-get install phpmyadmin
Default folder /usr/share/phpMyAdmin, above mysql root password is 123456.
$ sudo apt-get install php7.x-cgi // If "php-cgi not found"
4. 安装 python & pip
Python 2.7.x
$ sudo apt-get install python-dev python-pip
Python 3.x.x
$ sudo apt-get install python3-dev python3-pip
#######################
国外的pip源连接不稳定,使用阿里的pip源:
新建 ~/.pip/pip.conf
$ vim pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
5. 安装 Django 3.x
1)安装 MySQL client 支持
$ sudo apt-get install libmysqlclient-dev (Python 3.6.9, 有效)
$ sudo apt-get install python3-mysqldb (Python 3.8.0,有效)
$ sudo apt-get install python-mysqldb (Python 2.x, 没有测试过)
$ sudo pip install mysqlclient (Django 3.0 以上, 用这个)
$ sudo pip install pymysql (Django 2.x, 试试这个)
2)直接安装 Django
$ sudo pip install Django==3.0
3)虚拟环境下安装 Django
可以用virtualenv创建一个新的虚拟环境,安装所需的 python包。这样就能确保我们的包配置与我们正在开展的任何其他项目保持良好的隔离。
(1)安装 virtualenv
$ sudo pip3 install virtualenv
(2)配置 virtualenv 环境
$ find ~/ -name virtualenv
/home/xxx/.local/bin/virtualenv
$ cat << EOF > ~/a
> export PATH=$PATH:/home/xxx/.local/bin
> EOF
$ virtualenv env
$ source env/bin/activate
$ deactivate # 退出虚拟环境
6. 安装 mod-wsgi, 设置 Django + Apache2
---------- Apache2 WSGI ----------
$ sudo apt-get install libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3 # For Python 3
$ vim /etc/apache2/apache2.conf
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so-3.x # For Python 3
在apache2.conf或 vHost配置文件里添加如下内容:
<VirtualHost *:8080>
ServerName localhost
ServerAlias localhost_test
DocumentRoot /home/xxx/workshop/django/project-name
# DirectoryIndex index.html
# Media files
#Alias /media/ /home/xxx/workshop/django/project-name/media/
# Static files (js/css/images)
Alias /static/ /home/xxx/workshop/django/project-name/static/
<Directory /home/xxx/workshop/django/project-name/static/>
Require all granted
</Directory>
# WSGI
WSGIScriptAlias / /home/xxx/workshop/django/project-name/project-name/wsgi.py
<Directory "/home/xxx/workshop/django/project-name/project-name/">
Require all granted
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
7. 安装 Django restFramework & Oauth2.0 (开发RESTFUL时需要,非必选)
$ sudo pip install djangorestframework
$ sudo pip install djangorestframework-xml (optional)
$ sudo pip install django-oauth-toolkit (optional, djangorestframework-oauth)
8. 安装 CORS for Django (开发跨域访问时需要,非必选)
$ sudo pip install django-cors-headers
9. 安装 Captcha (Form提交数据的简单图形验证,非必选)
$ sudo pip install django-simple-captcha
二. 创建 Django 项目
1. 创建
运行命令行:
$ django-admin startproject 'project-name'
2. 运行项目
进入项目 'project-name' 目录, 运行:
$ python manage.py runserver
用浏览器访问:http://localhost:8000, 可以指定 Host 和 Port,如:
$ python manage.py runserver 192.168.0.1:8080
3. 添加 app
进入项目 'project-name' 目录, 运行:
$ python manage.py startapp 'app-name'
4. 配置 MySQL
修改 settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '3306',
}
}
运行命令行:
$ python manage.py makemigrations 'app-name'
$ python manage.py migrate
5. Django shell
$ python manage.py shell
6. Django Admin
$ python manage.py createsuperuser
7. 支持多语言(I18n)
Django 2.2.x 下测试过,3.x 没测试过。
1) 修改 views.py
from django.utils.translation import gettext_lazy as _
_('Demo text')
2) 修改模版 HTML
略
3) 修改 setting.py:
#LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-hans'
# Add locale paths
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
4) 生成Message文件
$ mkdir locale
$ django-admin makemessages -l zh_Hans
$ django-admin compilemessages