更新软件包
1 | dnf check-update |
安装Python3和pip
1 | dnf install python3 python3-pip |
通过pip安装Django
1 | pip3 install Django |
验证Django安装是否成功
1 | django-admin --version |
创建一个Django项目
1. 创建项目
django-admin startproject demo1
1 | tree |
2. 创建APP
python3 manage.py startapp app01
1 | tree |
3. 快速上手,写一个页面
编辑
demo1/settings.py
, INSTALLED_APPS中添加’app01.apps.App01Config’1
2
3
4
5
6
7# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
# ...
'app01.apps.App01Config', # Add your app config here !
]编辑
demo1/urls.py
, 添加URL和视图函数的映射1
2
3
4from app01 import views
urlpatterns = [
path('index/', views.index),
]编辑
app01/views.py
1
2
3
4from django.shortcuts import render, HttpResponse
def index(request):
return HttpResponse("Hello World")
4. 启动项目,测试
启动:python3 manage.py runserver 0.0.0.0:8000
测试:curl localhost:8000/index/
参考
https://docs.djangoproject.com/zh-hans/5.1/topics/install/#installing-official-release