[python:django]:web框架搭建项目

pip查看安装列表

C:\Users\30252>pip list
Package           Version
----------------- -----------
cycler            0.11.0
fonttools         4.38.0
joblib            1.3.2
kiwisolver        1.4.5
matplotlib        3.5.3
numpy             1.21.6
packaging         24.0
pandas            1.1.5
Pillow            9.5.0
pip               10.0.1
pyparsing         3.1.2
python-dateutil   2.9.0.post0
pytz              2024.1
scikit-learn      1.0.2
scipy             1.7.3
setuptools        39.0.1
six               1.16.0
threadpoolctl     3.1.0
typing-extensions 4.7.1
Cache entry deserialization failed, entry ignored
You are using pip version 10.0.1, however version 24.0 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command

安装制定Django版本

pip install Django==2.2.4

C:\Users\30252>pip install Django==2.2.4
Collecting Django==2.2.4
  Downloading https://files.pythonhosted.org/packages/d6/57/66997ca6ef17d2d0f0ebcd860bc6778095ffee04077ca8985928175da358/Django-2.2.4-py3-none-any.whl (7.5MB)
    100% |████████████████████████████████| 7.5MB 5.7MB/s
Requirement already satisfied: pytz in d:\dev\env\python\lib\site-packages (from Django==2.2.4) (2024.1)
Collecting sqlparse (from Django==2.2.4)
  Downloading https://files.pythonhosted.org/packages/98/5a/66d7c9305baa9f11857f247d4ba761402cea75db6058ff850ed7128957b7/sqlparse-0.4.4-py3-none-any.whl (41kB)
    100% |████████████████████████████████| 51kB 6.6MB/s
Installing collected packages: sqlparse, Django
  The script sqlformat.exe is installed in 'd:\dev\env\python\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  The script django-admin.exe is installed in 'd:\dev\env\python\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed Django-2.2.4 sqlparse-0.4.4
You are using pip version 10.0.1, however version 24.0 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

初始化django项目

django-admin startproject compute

E:\>cd \WorkContent\shanghaikaifangdaxue\pythonweb\pythonProject1\test-two-django

E:\WorkContent\shanghaikaifangdaxue\pythonweb\pythonProject1\test-two-django>django-admin startproject compute

E:\WorkContent\shanghaikaifangdaxue\pythonweb\pythonProject1\test-two-django>

初始化后项目结构

在这里插入图片描述

执行 python manage.py startapp projectName 生成app应用

python manage.py startapp app 执行完命令,根目录生成app文件夹
在这里插入图片描述

执行 python manage.py runserver 运行web项目

python manage.py runserver

PS E:\WorkContent\shanghaikaifangdaxue\pythonweb\pythonProject1\test-two-django\compute> python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 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.
May 12, 2024 - 05:51:19
Django version 2.2.4, using settings 'compute.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[12/May/2024 05:51:33] "GET / HTTP/1.1" 200 16348
[12/May/2024 05:51:33] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[12/May/2024 05:51:33] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
[12/May/2024 05:51:33] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[12/May/2024 05:51:33] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
Not Found: /favicon.ico
[12/May/2024 05:51:33] "GET /favicon.ico HTTP/1.1" 404 1973

在这里插入图片描述

配置django项目页面访问地址

  1. 根目录创建template文件夹,创建index.html页面
  2. app目录views.py配置渲染的页面地址路径:
from django.shortcuts import render

def home(request):
    return render(request, 'index.html')
  1. compute文件夹下的urls.py配置
 urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home, name='home')
]

注意:

注意

  1. urls.py配置里面第一个参数控制页面访问路径对应的具体views里面的函数模块返回的return render
  2. urls.py 里的home模块需要导入 from app.views import home
    home 是在 views.py 里面定义的 def home() 函数 return render(request, ‘index.html’) 渲染返回的地址

在这里插入图片描述
在这里插入图片描述

再次访问地址,返回制定页面

在这里插入图片描述

相关推荐

  1. vue项目---1.基础的框架

    2024-05-13 11:04:04       31 阅读
  2. 小程序开发之uniapp项目框架

    2024-05-13 11:04:04       43 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-13 11:04:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-13 11:04:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-13 11:04:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-13 11:04:04       20 阅读

热门阅读

  1. 开发一款抓大鹅游戏

    2024-05-13 11:04:04       14 阅读
  2. Debug: Pytorch dataloaders OSError: Bad file descriptor

    2024-05-13 11:04:04       15 阅读
  3. leetcode题目7

    2024-05-13 11:04:04       14 阅读
  4. 【二叉树算法题记录】404. 左叶子之和

    2024-05-13 11:04:04       12 阅读
  5. 安卓LeakCanary研究

    2024-05-13 11:04:04       15 阅读
  6. SQLite 语法大全

    2024-05-13 11:04:04       14 阅读
  7. codeforce#939 (div2) 题解

    2024-05-13 11:04:04       28 阅读
  8. 什么是结对编程?

    2024-05-13 11:04:04       12 阅读
  9. Caffe: Convolutional Architecture for Fast Feature Embedding

    2024-05-13 11:04:04       14 阅读
  10. Docker 部署Redis

    2024-05-13 11:04:04       17 阅读