1. 检查安装 Python
由于树莓派已经安装了Python,所以基本检查下配置就可以了
pi@pi-sql:~ $ mkdir Django
pi@pi-sql:~ $ cd Django/
pi@pi-sql:~/Django $ python
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
pi@pi-sql:~/Django $
如果没有Python 可以用下面命令安装
pi@pi-sql:~/Django $ sudo apt-get install python
pi@pi-sql:~/Django $ sudo apt-get install python3
建立默认 Python 连接
pi@pi-sql:~/Django $ python
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit()
pi@pi-sql:~/Django $ sudo rm /usr/bin/python
pi@pi-sql:~/Django $ sudo ln -s /usr/bin/python3 /usr/bin/python
python3 python3.9 python3.9-config python3-config
pi@pi-sql:~/Django $ sudo ln -s /usr/bin/python3.9 /usr/bin/python
pi@pi-sql:~/Django $
如果国内安装缓慢可以用国内源
阿里云 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣 http://pypi.douban.com/simple/
Python官方 https://pypi.python.org/simple/
v2ex http://pypi.v2ex.com/simple/
中国科学院 http://pypi.mirrors.opencas.cn/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
pi@raspberrypi:~ $ sudo pip install Django -i https://pypi.tuna.tsinghua.edu.cn/simple
pi@raspberrypi:~ $ sudo pip install Django
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: Django in /usr/local/lib/python3.9/dist-packages (4.1.4)
Requirement already satisfied: asgiref<4,>=3.5.2 in /usr/local/lib/python3.9/dist-packages (from Django) (3.6.0)
Requirement already satisfied: sqlparse>=0.2.2 in /usr/local/lib/python3.9/dist-packages (from Django) (0.4.3)
pi@raspberrypi:~ $
2. 检查安装情况
pi@raspberrypi:~ $ python
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(4, 1, 4, 'final', 0)
>>> exit()
pi@raspberrypi:~ $
3. 安装虚拟环境
创建一个目录 Django
pi@pi-sql:~ $ rm -r Django/
pi@pi-sql:~ $ mkdir Django
pi@pi-sql:~ $ cd Django
pi@pi-sql:~/Django $ ls
pi@pi-sql:~/Django $
Django 目录下创建创建虚拟环境 ven1
pi@pi-sql:~/Django $ python -m venv ven1
pi@pi-sql:~/Django $ ls
ven1
pi@pi-sql:~/Django $
启动虚拟环境,注意前面的ven1代表虚拟环境
退出用 deactivate.
pi@pi-sql:~/Django $ source ven1/bin/activate
(ven1) pi@pi-sql:~/Django $
(ven1) pi@pi-sql:~/Django $ deactivate
pi@pi-sql:~/Django $ source ven1/bin/activate
(ven1) pi@pi-sql:~/Django $
4. 在虚拟环境下安装 Django
(ven1) pi@pi-sql:~/Django $ pip install django
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting django
Downloading https://www.piwheels.org/simple/django/Django-4.1.7-py3-none-any.w hl (8.1 MB)
|████████████████████████████████| 8.1 MB 24 kB/s
Collecting sqlparse>=0.2.2
Downloading https://www.piwheels.org/simple/sqlparse/sqlparse-0.4.3-py3-none-a ny.whl (42 kB)
|████████████████████████████████| 42 kB 100 kB/s
Collecting asgiref<4,>=3.5.2
Downloading asgiref-3.6.0-py3-none-any.whl (23 kB)
Installing collected packages: sqlparse, asgiref, django
Successfully installed asgiref-3.6.0 django-4.1.7 sqlparse-0.4.3
(ven1) pi@pi-sql:~/Django $
5. 创建第一个 Django 项目 myproject
由于虚拟环境是后期搭建的所以下面的所有pi前都带有(venv)
(ven1) pi@pi-sql:~ $ cd Django
(ven1) pi@pi-sql:~/Django $ django-admin startproject myproject
(ven1) pi@pi-sql:~/Django $ ls
myproject
(ven1) pi@raspberrypi:~/Django $ cd myproject/
(ven1) pi@raspberrypi:~/Django/myproject $ ls
manage.py myproject
(ven1) pi@raspberrypi:~/Django/myproject $
6. Django 测试运行 。
python manage.py runserver 0.0.0.0:8000
然后浏览器打开树莓派的网址,提示错误,权限没有设置正确:
(ven1) pi@pi-sql:~/Django/myproject $ python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 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.
February 25, 2023 - 06:18:49
Django version 4.1.7, using settings 'myproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
6. 修改权限 。
按Ctrl+C中断,然后
pi@raspberrypi:~/Django/myproject $ tree
.
├── db.sqlite3
├── manage.py
└── myproject
├── asgi.py
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-39.pyc
│ ├── settings.cpython-39.pyc
│ ├── urls.cpython-39.pyc
│ └── wsgi.cpython-39.pyc
├── settings.py
├── urls.py
└── wsgi.py
2 directories, 11 files
按Ctrl+X, Y, 保存退出
打开配置文件,修改ALLOWED_HOSTS
pi@raspberrypi:~/Django/myproject $ nano myproject/settings.py
修改:ALLOWED_HOSTS = []
修改:ALLOWED_HOSTS = ['*']
按Ctrl+X, Y, 保存退出
7. Django 再次运行 。
python manage.py runserver 0.0.0.0:8000
然后浏览器打开树莓派的网址 http://192.168.1.101:8000/:
一切正常:
pi@raspberrypi:~/Django/myproject $ python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 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 29, 2022 - 02:58:01
Django version 4.1.4, using settings 'myproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[29/Dec/2022 02:58:22] "GET / HTTP/1.1" 200 10681
[29/Dec/2022 02:58:22] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 304 0
网页:
8. Django 创建第一App App1。
(ven1) pi@pi-sql:~/Django/myproject $ python manage.py startapp app1
(ven1) pi@pi-sql:~/Django/myproject $ tree
.
├── app1
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── manage.py
└── myproject
├── asgi.py
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-39.pyc
│ ├── settings.cpython-39.pyc
│ ├── urls.cpython-39.pyc
│ └── wsgi.cpython-39.pyc
├── settings.py
├── urls.py
└── wsgi.py
4 directories, 18 files
(ven1) pi@pi-sql:~/Django/myproject $
8.1, 编辑View。
(ven1) pi@pi-sql:~/Django/myproject $ nano app1/views.py
输入下面内容,Ctrl+X, 然后按Y,保存退出。
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
8.2 新建 app1/urls.py 。
新建 app1/urls.py 。
(ven1) pi@pi-sql:~/Django/myproject $ nano app1/urls.py
输入下面内容,Ctrl+X, 然后按Y,保存退出。
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
8.3 编辑 myproject/urls.py 。
打开 myproject/urls.py 。
(ven1) pi@pi-sql:~/Django/myproject $ nano myproject/urls.py
输入下面内容,Ctrl+X, 然后按Y,保存退出。
注意在原有内容上增加:include,path('app1/', views.index, name='index'),
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('app1/', include('app1.urls')),
]
8.3 再次运行 。
(ven1) pi@pi-sql:~/Django/myproject $ python manage.py runserver 0.0.0.0:8000
浏览器输入:http://192.168.1.101:8000/
将会看到:
Page not found (404)
Request Method: GET
Request URL: http://192.168.1.101:8000/
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
admin/
app1/
The empty path didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
浏览器输入:http://192.168.1.101:8000/admin/
浏览器输入:http://192.168.1.101:8000/app1/
就可以看到管理员登录界面和自己的 App1页面。
9 . Django 创建管理员账号 。
解决错误提示
You have 18 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 29, 2022 - 03:47:59
(ven1) pi@pi-sql:~/Django/myproject $ python 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 admin.0003_logentry_add_action_flag_choices... 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 auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK
pi@raspberrypi:~/Django/myproject $
pi@raspberrypi:~/Django/myproject $ python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
December 29, 2022 - 03:52:04
Django version 4.1.4, using settings 'myproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
创建管理员账号 root, 注意密码有足够复杂。
pi@raspberrypi:~/Django/myproject $ python manage.py createsuperuser
Username (leave blank to use 'pi'): root
Email address: xxxxxx@qq.com
Password:
Password (again):
The password is too similar to the email address.
This password is entirely numeric.
Bypass password validation and create user anyway? [y/N]: n
Password:
Password (again):
Superuser created successfully.
pi@raspberrypi:~/Django/myproject $
浏览器输入, 输入刚才的用户名和密码:
http://192.168.1.101:8000/admin
管理界面就出现了。
10. 安装 uwsgi
发现用了内网穿透完全可以不用这个,暂时没有做这个功能
11. VS Code 快速开发网页
下一步将研究如何将VS Code的项目快速的移动到树莓派上。