Django学习(一)

Django起源:

项目创建:

项目执行(测试开发阶段):

项目结束:

结构:

db.sqlite3:数据库,一般不用,会替换成我们自己的数据库

manage.py

项目同名目录:

settings.py
settings中的配置详解:

注意:添加自定义配置时名称必须大写

django处理url的过程

视图函数

路由配置

path转换器

re_path

Django中的请求

request下的函数

django响应对象:

代码展示:

urls.py

python 复制代码
from django.contrib import admin
from django.urls import path
from . import views
from django.urls import re_path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('page/2004/', views.page_2003_view),
    path('', views.index_view),
    path('page/1', views.page_1_view),
    path('page/2', views.page_2_view),
    path('page/<int:pg>', views.page_view),
    # 正则表达式匹配
    # x为数字(1位和两位),op为字母,y为数字(1位和两位),$表示结束
    re_path(r'^(?P<x>\d{1,2})/(?P<op>\w+)/(?P<y>/d{1,2})$', views.cal2_view),
    path('<int:n>/<str:op>/<int:m>', views.cal_view),
    re_path(r'^birthday/(?P<b>\d{4})/(?P<c>\d{1,2})/(?P<d>\d{1,2})$', views.date_view),
    path('aa/', views.test_request)
]

views.py

python 复制代码
from django.http import HttpResponse


def page_2003_view(request):
    html = "<h1>这是一个页面</h1>"
    return HttpResponse(html)


def index_view(request):
    html = "<h1>这是一个首页</h1>"
    return HttpResponse(html)


def page_1_view(request):
    html = "<h1>这是编号为1的页面</h1>"
    return HttpResponse(html)


def page_2_view(request):
    html = "<h1>这是编号为2的页面</h1>"
    return HttpResponse(html)


def page_view(request, pg):
    html = f"<h1>这是编号为{pg}的页面</h1>"
    return HttpResponse(html)


def cal_view(request, n, op, m):
    if op not in ['add', 'sub', 'mul']:
        return HttpResponse('Your op is wrong')
    result = 0
    if op == 'add':
        result = n + m
    elif op == 'sub':
        result = n - m
    elif op == 'mul':
        result = n * m
    return HttpResponse(f'结果为{result}')


def cal2_view(request, x, op, y):
    if op not in ['add', 'sub', 'mul']:
        return HttpResponse('Your op is wrong')
    result = 0
    if op == 'add':
        result = x + y
    elif op == 'sub':
        result = x - y
    elif op == 'mul':
        result = x * y
    return HttpResponse(f'结果shi{result}')


def date_view(request, a, b, c, d):
    html = f"<h1>生日为{b}-{c}-{d}</h1>"
    return HttpResponse(html)


def test_request(request):
    print("url字符集:", request.path_info)
    print("method is", request.method)
    print("querystring is", request.GET)
    print(request.get_full_path())
    return HttpResponse("html")

请求过程:

服务起来之后,输入urls.py中配置的连接,就会跳转到链接对应的在views.py文件中编写的视图对象

相关推荐
爱喝喜茶爱吃烤冷面的小黑黑2 分钟前
小黑一层层削苹果皮式大模型应用探索:langchain中智能体思考和执行工具的demo
python·langchain·代理模式
傍晚冰川24 分钟前
FreeRTOS任务调度过程vTaskStartScheduler()&任务设计和划分
开发语言·笔记·stm32·单片机·嵌入式硬件·学习
月初,1 小时前
MongoDB学习和应用(高效的非关系型数据库)
学习·mongodb·nosql
Blossom.1181 小时前
使用Python和Flask构建简单的机器学习API
人工智能·python·深度学习·目标检测·机器学习·数据挖掘·flask
casual_clover1 小时前
Android 之 kotlin 语言学习笔记四(Android KTX)
android·学习·kotlin
Love__Tay2 小时前
【学习笔记】Python金融基础
开发语言·笔记·python·学习·金融
有风南来3 小时前
算术图片验证码(四则运算)+selenium
自动化测试·python·selenium·算术图片验证码·四则运算验证码·加减乘除图片验证码
wangjinjin1803 小时前
Python Excel 文件处理:openpyxl 与 pandas 库完全指南
开发语言·python
我的golang之路果然有问题3 小时前
云服务器部署Gin+gorm 项目 demo
运维·服务器·后端·学习·golang·gin
Yxh181377845543 小时前
抖去推--短视频矩阵系统源码开发
人工智能·python·矩阵