django(三):创建第一个django的app和url的两种获取参数的方式

一.创建第一个django的app

1.1 创建app的方式

1.1.1 使用命令行创建APP

在终端根目录下执行命令

python 复制代码
## 创建一个app     
python manage.py startapp <app名称>

例:python manage.py startapp book
1.1.2 使用pycharm创建APP

1.2创建成功后生成文件如下

二.url两种获取参数的方式

book/views.py

python 复制代码
from django.shortcuts import render, HttpResponse

# Create your views here.
"""
从url中获取参数
1.查询字符串(query_string):http://localhost:8000/book?name=xxx&age=xxx&className=xxx
2.从path获取:http://localhost:8000/book/xxxx
"""


# 1.查询字符串(query_string):http://localhost:8000/book?name=lsy&age=23&className=幼儿园中班

def book_query_string(request):
    """
    :param request: name,age,class_name
    :return: 个人介绍
    """
    name = request.GET.get('name')
    age = request.GET.get('age')
    class_name = request.GET.get('className')
    return HttpResponse(f"我的名字是{name},今年{age}岁了,我是{class_name}的")


# 2.从path获取:http://localhost:8000/book/10

def book_detail(request, book_id):
    """
    :param book_id:
    :param request: id:book_id
    :return:当前book的id
    """
    return HttpResponse(f"这本书的id是{book_id}")

djangoProject/urls.py

python 复制代码
"""
URL configuration for djangoProject project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/5.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.shortcuts import HttpResponse

from book.views import book_query_string, book_detail

"""
django里的第一个函数必须是request,不写会报错
"""


def index(request):
    return HttpResponse("Hello, world. You're at the index of djangoProject.")


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', index),  # 不写path的第一个参数  那么就是/
    path('test', index),  # 默认情况下是自带/路径的  在这里定义test访问/test 可以访问
    path('book', book_query_string),  # 往里传参数 如果参数没有  view里还使用了 那么会返回None  book_detail
    path('book/<int:book_id>', book_detail),  # <boo_id> 表示/book/id的值    <int:book_id> 的int 表示他只接收int类型的参数,否贼会报404
]

2.1 通过查询字符串(query_string):http://localhost:8000/book?name=lsy\&age=23\&className=幼儿园中班

2.2 通过从path获取:http://localhost:8000/book/10

2.2.1 path里的类型限制

注意:

path里面定义的int 所以他后面必须跟的是整数类型,如果填写其他类型 那么将会报错404

相关推荐
ynchyong37 分钟前
Python 字符组合生成器:product 与 permutations 的实战对比
python·工具·字符串生产
benchmark_cc38 分钟前
1000只ETF的5分钟K线如何批量获取?QuantDash分页策略与高性能Python实践
开发语言·人工智能·爬虫·python·算法·quantdash·量化数据源
sel_944 分钟前
【PEFT】参数高效微调(PEFT)技术详解:从原理到 LoRA/QLoRA 实战
人工智能·python·深度学习·算法·机器学习·参数高效微调
月光船幽幽1 小时前
带刺反馈引擎:唤醒深层意图
人工智能·python
学长毕业设计1 小时前
基于springboot的云南中草药知识普及管理网站的设计与开发(源码+文档+讲解视频)
java·spring boot·后端
大模型码小白1 小时前
AI 提示词专栏:使用系统指令(System Prompt)实现全局约束
java·大数据·运维·开发语言·人工智能·python·prompt
Mr数据杨2 小时前
【Codex】用座位划分模块优化班级空间与课堂管理
django·codex·项目开发
烂蜻蜓2 小时前
Flask入门教程(八):视图函数详解——请求处理与响应的核心
后端·python·flask
河南凹凸环境艺术设计2 小时前
民宿酒店设计实力公司
大数据·人工智能·python