安装pip:
sudo apt-get install python3-pip
安装Django:
sudo pip3 install Django==2.2.15 -i https://pypi.tuna.tsinghua.edu.cn/simple
安装好之后进入python命令行,查看是否安装成功以及版本号:
ubuntu:/usr/bin$ python3
Python 3.5.2 (default, Jul 17 2020, 14:04:10)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'2.2.15'
>>>
创建项目:
django-admin.py startproject HelloWorld
创建项目可能会出现错误,可能需要修改python的默认运行版本,在Linux中默认会安装几个python。我们这里装的是python3的django,所以把python运行版本改为3.5:
$ ll /usr/bin/python*
lrwxrwxrwx 1 root root 9 3月 5 10:13 /usr/bin/python -> python2.7*
lrwxrwxrwx 1 root root 9 3月 5 10:13 /usr/bin/python2 -> python2.7*
-rwxr-xr-x 1 root root 3488528 7月 22 01:57 /usr/bin/python2.7*
lrwxrwxrwx 1 root root 33 7月 22 01:57 /usr/bin/python2.7-config -> x86_64-linux-gnu-python2.7-config*
lrwxrwxrwx 1 root root 16 3月 5 10:13 /usr/bin/python2-config -> python2.7-config*
lrwxrwxrwx 1 root root 9 3月 5 10:13 /usr/bin/python3 -> python3.5*
-rwxr-xr-x 2 root root 4456208 7月 20 04:14 /usr/bin/python3.5*
lrwxrwxrwx 1 root root 33 7月 20 04:14 /usr/bin/python3.5-config -> x86_64-linux-gnu-python3.5-config*
-rwxr-xr-x 2 root root 4456208 7月 20 04:14 /usr/bin/python3.5m*
lrwxrwxrwx 1 root root 34 7月 20 04:14 /usr/bin/python3.5m-config -> x86_64-linux-gnu-python3.5m-config*
lrwxrwxrwx 1 root root 16 3月 23 2016 /usr/bin/python3-config -> python3.5-config*
lrwxrwxrwx 1 root root 10 3月 5 10:13 /usr/bin/python3m -> python3.5m*
lrwxrwxrwx 1 root root 17 3月 23 2016 /usr/bin/python3m-config -> python3.5m-config*
lrwxrwxrwx 1 root root 16 3月 5 10:13 /usr/bin/python-config -> python2.7-config*
lrwxrwxrwx 1 root root 58 3月 26 2016 /usr/bin/pythontex -> ../share/texlive/texmf-dist/scripts/pythontex/pythontex.py*
-rwxr-xr-x 1 root root 306 3月 26 2016 /usr/bin/pythontex3*
默认的python运行的是2.7版本。
删除这个软链接,新建即可:
bekl:/usr/bin$ sudo rm -rf python
bekl:/usr/bin$ sudo ln -s python3.5 python
再次查看python所有版本情况:
bekl:/usr/bin$ ll python*
lrwxrwxrwx 1 root root 9 8月 20 18:56 python -> python3.5*
lrwxrwxrwx 1 root root 9 3月 5 10:13 python2 -> python2.7*
-rwxr-xr-x 1 root root 3488528 7月 22 01:57 python2.7*
lrwxrwxrwx 1 root root 33 7月 22 01:57 python2.7-config -> x86_64-linux-gnu-python2.7-config*
lrwxrwxrwx 1 root root 16 3月 5 10:13 python2-config -> python2.7-config*
lrwxrwxrwx 1 root root 9 3月 5 10:13 python3 -> python3.5*
-rwxr-xr-x 2 root root 4456208 7月 20 04:14 python3.5*
lrwxrwxrwx 1 root root 33 7月 20 04:14 python3.5-config -> x86_64-linux-gnu-python3.5-config*
-rwxr-xr-x 2 root root 4456208 7月 20 04:14 python3.5m*
lrwxrwxrwx 1 root root 34 7月 20 04:14 python3.5m-config -> x86_64-linux-gnu-python3.5m-config*
lrwxrwxrwx 1 root root 16 3月 23 2016 python3-config -> python3.5-config*
lrwxrwxrwx 1 root root 10 3月 5 10:13 python3m -> python3.5m*
lrwxrwxrwx 1 root root 17 3月 23 2016 python3m-config -> python3.5m-config*
lrwxrwxrwx 1 root root 16 3月 5 10:13 python-config -> python2.7-config*
lrwxrwxrwx 1 root root 58 3月 26 2016 pythontex -> ../share/texlive/texmf-dist/scripts/pythontex/pythontex.py*
-rwxr-xr-x 1 root root 306 3月 26 2016 pythontex3*
接下来,进入学习阶段:
https://www.runoob.com/django/django-first-app.html
在创建的工程当中,注意几个需要改动的地方。
一:在项目中创建templates文件夹,用于放入html文件,将setting.py中的TEMPLATES中的‘DIRS'值改为[BASE_DIR+"/templates",],
二:将setting.py中的ALLOWED_HOSTS值改为'*',并将参数IP改为0.0.0.0,允许所有的IP进行访问。否则只能本机访问: