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文件中编写的视图对象

相关推荐
IT古董31 分钟前
第四章:大模型(LLM)】06.langchain原理-(3)LangChain Prompt 用法
java·人工智能·python
冷崖3 小时前
MySQL异步连接池的学习(五)
学习·mysql
知识分享小能手3 小时前
Vue3 学习教程,从入门到精通,Axios 在 Vue 3 中的使用指南(37)
前端·javascript·vue.js·学习·typescript·vue·vue3
fantasy_arch5 小时前
pytorch例子计算两张图相似度
人工智能·pytorch·python
WBluuue7 小时前
数学建模:智能优化算法
python·机器学习·数学建模·爬山算法·启发式算法·聚类·模拟退火算法
焄塰7 小时前
Ansible 管理变量和事实
学习·centos·ansible
赴3357 小时前
矿物分类案列 (一)六种方法对数据的填充
人工智能·python·机器学习·分类·数据挖掘·sklearn·矿物分类
大模型真好玩7 小时前
一文深度解析OpenAI近期发布系列大模型:意欲一统大模型江湖?
人工智能·python·mcp
RPA+AI十二工作室7 小时前
亚马逊店铺绩效巡检_影刀RPA源码解读
chrome·python·rpa·影刀
oe10198 小时前
读From GPT-2 to gpt-oss: Analyzing the Architectural Advances(续)
笔记·gpt·学习