如何在Ubuntu 16.04上安装Django Web Framework 2

时间:2020-01-09 10:39:26  来源:igfitidea点击:

如何在Ubuntu Linux 16.04 LTS服务器上安装Django?
如何为Django 2.x创建示例Hello世界视图和/admin /部分?

Django是用于创建Web应用程序的免费开放源代码Python Web框架。
在本教程中,您将学习如何在Ubuntu Linux 16.04 LTS服务器上安装Django 2,包括适用于您站点的快速入门。

安装最新版本的Django 2.x

首先,您需要为Python 3.x安装pip。
执行以下apt命令/apt-get命令

$ sudo apt-get install python3-pip

输出示例:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  binutils build-essential ca-certificates cpp cpp-5 dpkg-dev fakeroot g++ g++-5 gcc gcc-5 libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan2 libatomic1 libc-dev-bin libc6-dev
  libcc1-0 libcilkrts5 libdpkg-perl libexpat1-dev libfakeroot libfile-fcntllock-perl libgcc-5-dev libgdbm3 libgomp1 libisl15 libitm1 liblsan0 libmpc3 libmpfr4 libmpx0 libperl5.22 libpython3-dev
  libpython3.5-dev libquadmath0 libstdc++-5-dev libtsan0 libubsan0 linux-libc-dev make manpages manpages-dev openssl patch perl perl-modules-5.22 python-pip-whl python3-dev python3-pkg-resources
  python3-setuptools python3-wheel python3.5-dev rename xz-utils
Suggested packages:
  binutils-doc cpp-doc gcc-5-locales debian-keyring g++-multilib g++-5-multilib gcc-5-doc libstdc++6-5-dbg gcc-multilib autoconf automake libtool flex bison gdb gcc-doc gcc-5-multilib libgcc1-dbg libgomp1-dbg
  libitm1-dbg libatomic1-dbg libasan2-dbg liblsan0-dbg libtsan0-dbg libubsan0-dbg libcilkrts5-dbg libmpx0-dbg libquadmath0-dbg glibc-doc libstdc++-5-doc make-doc man-browser ed diffutils-doc perl-doc
  libterm-readline-gnu-perl | libterm-readline-perl-perl python-setuptools-doc
The following NEW packages will be installed:
  binutils build-essential ca-certificates cpp cpp-5 dpkg-dev fakeroot g++ g++-5 gcc gcc-5 libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan2 libatomic1 libc-dev-bin libc6-dev
  libcc1-0 libcilkrts5 libdpkg-perl libexpat1-dev libfakeroot libfile-fcntllock-perl libgcc-5-dev libgdbm3 libgomp1 libisl15 libitm1 liblsan0 libmpc3 libmpfr4 libmpx0 libperl5.22 libpython3-dev
  libpython3.5-dev libquadmath0 libstdc++-5-dev libtsan0 libubsan0 linux-libc-dev make manpages manpages-dev openssl patch perl perl-modules-5.22 python-pip-whl python3-dev python3-pip python3-pkg-resources
  python3-setuptools python3-wheel python3.5-dev rename xz-utils
0 upgraded, 57 newly installed, 0 to remove and 0 not upgraded.
Need to get 86.3 MB of archives.
After this operation, 245 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu xenial/main amd64 libgdbm3 amd64 1.8.3-13.1 [16.9 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 perl-modules-5.22 all 5.22.1-9ubuntu0.2 [2661 kB]
...
....
..

使用以下pip3命令安装django

$ sudo pip3 install Django

或者

$ sudo pip3 install Django==2.0

使用pip/pip3命令安装Django 2.x

通过执行以下简单命令来验证安装:

$ django-admin --version

或者

$ python3 -m django --version

输出示例:

2.0

为新项目创建用户帐户

使用useradd命令为我的项目创建一个名为igidjango的新用户:

$ sudo useradd -c 'theitroad.local app root' -d /home/httpd/ -m -s /bin/bash igidjango
$ sudo passwd igidjango

其中:

  • -c'description':为新帐户设置GECOS
  • -d/home/httpd /:为新帐户设置主目录
  • -m:为新帐户创建主目录
  • -s/bin/bash:为新帐户设置bash shell

将django 2.x代码放在文档根目录之外的某个目录中,例如/home/httpd而不是默认的/var/www/html。
这是一项安全功能。
现在您可以使用ssh命令或su命令登录

$ ssh [email protected]

$su igidjango

编写第一个Django 2.x应用

首先,您需要使用django-admin命令自动生成一些代码来建立Django 2.x项目。
语法为:

$ django-admin startproject Your-Project-Name-Here
$ cd Your-Project-Name-Here

例如,我要创建一个名为helloworld的项目

$ django-admin startproject helloworld
$ ls -l

上面将创建一个目录演示,其布局如下:

接下来,通过在helloworld目录中执行以下命令来启动开发服务器:

$ python3 manage.py runserver

输出示例:

Performing system checks...
 
System check identified no issues (0 silenced).
 
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
 
December 03, 2016 - 17:13:44
Django version 2.0, using settings 'helloworld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

要更改服务器的IP和端口,请运行:

$ python3 manage.py runserver 8443
$ python3 manage.py runserver 0.0.0.0:8443
$ python3 manage.py runserver 10.98.222.14:8443

如果更改端口和IP,请确保按照以下方式更新settings.py,以避免出现以下错误:

DisallowedHost at /
Invalid HTTP_HOST header: '10.98.222.14:8443'. You may need to add '10.98.222.14' to ALLOWED_HOSTS.
http://10.98.222.14:8443

编辑helloworld/settings.py

$ vi helloworld/settings.py

设置/更新/编辑ALLOWED_HOSTS如下:

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '192.168.1.115', '10.98.222.14' ]

保存并关闭文件。
再次启动服务器:

$ python3 manage.py runserver 10.98.222.14:8443

打开浏览器,然后输入以下网址:

http://10.98.222.14:8443

撰写您的第一张观点

首先,请确保您位于与manage.py相同的目录中,然后执行以下命令:

$ python3 manage.py startapp demo
$ ls -l

输出示例:
创建演示应用程序

执行您之前创建的管理员用户名和密码。
您应该看到网站的管理员部分,如下所示:

demo
demo/models.py
demo/apps.py
demo/tests.py
demo/views.py
demo/migrations
demo/migrations/__init__.py
demo/admin.py
demo/__init__.py

此目录结构将保存演示应用程序。
下一个编辑文件如下:

$ vi demo/views.py

编辑/追加如下:

from django.shortcuts import render
 
# Create your views here.
from django.http import HttpResponse
 
 
def index(request):
    return HttpResponse("Hello, world. Yay! My first app is online.")

保存并关闭文件。
这是Django中最简单的视图,并且简单地显示Hello,world。

要调用该视图,您需要将其映射到URL

$ vi demo/urls.py

追加以下代码:

from django.urls import path
 
from . import views
 
urlpatterns = [
    path('', views.index, name='index'),
]

保存并关闭文件。
最后在您的项目目录中更新urls.py:

$ vi helloworld/urls.py

编辑/追加如下:

"""helloworld URL Configuration
 
The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
 
urlpatterns = [
    path('demo/', include('demo.urls')),
    path('admin/', admin.site.urls),
]

保存并关闭文件。
再次启动服务器:

$ python3 manage.py runserver 10.98.222.14:8443

触发浏览器并输入以下命令url:

http://10.98.222.14:8443/demo/

如何引导数据库并为应用程序创建/admin /部分

执行以下命令以使用默认的sqlite3数据库引擎:

$ python3 manage.py migrate

输出示例:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK

创建一个管理员用户

执行以下命令:

$ python3 manage.py createsuperuser

输出示例:

Username (leave blank to use 'igidjango'): admin
Email address: [email protected]
Password: 
Password (again): 
Superuser created successfully.

启动开发服务器,运行:

$ python3 manage.py runserver 10.98.222.14:8443

触发Web浏览器,然后转到本地IP /域上的/admin /网址:

http://10.98.222.14:8443/admin/