路由器整形变量

This commit is contained in:
mxjdi 2025-04-26 22:10:07 +08:00
parent ab3886b373
commit b9a3cad0a4
6 changed files with 28 additions and 3 deletions

View File

@ -16,7 +16,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" /> <excludeFolder url="file://$MODULE_DIR$/.venv" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="Python 3.12" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
<component name="TemplatesService"> <component name="TemplatesService">

View File

@ -3,5 +3,5 @@
<component name="Black"> <component name="Black">
<option name="sdkName" value="Python 3.12 (Django5)" /> <option name="sdkName" value="Python 3.12 (Django5)" />
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (Django5)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12" project-jdk-type="Python SDK" />
</project> </project>

View File

@ -24,6 +24,20 @@ import helloWorld.views
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('index/', helloWorld.views.index), path('index/', helloWorld.views.index),
# 路由变量类型有字符类型整型slug和uuid
# 字符类型:匹配任何非空字符串,但不包含斜杆,如果没有指定类型,默认使用该类型
# 整形匹配0和正整数
# slug可理解为注释、后缀或附属等概念常作为路由的解析性字符
# uuid匹配一个uuid格式的对象为了防止冲突必须使用小写字母
# 使用字符类型
# http://localhost:8000/blog/22
path('blog/<int:id>', helloWorld.views.blog),
# 加上日期
#http://localhost:8000/blog2/2025/4/26/80
path('blog2/<int:year>/<int:month>/<int:day>/<int:id>', helloWorld.views.blog2),
# 媒体路由 # 媒体路由
re_path('media/(?P<path>.*)', serve, {'document_root': settings.MEDIA_ROOT}, name='media'), re_path('media/(?P<path>.*)', serve, {'document_root': settings.MEDIA_ROOT}, name='media'),
] ]

View File

View File

@ -7,4 +7,7 @@
<body> <body>
应用内模板 应用内模板
</body> </body>
</html> </html>

View File

@ -1,3 +1,4 @@
from django.http import HttpResponse
from django.shortcuts import render from django.shortcuts import render
@ -5,3 +6,10 @@ from django.shortcuts import render
def index(request): def index(request):
print("页面请求处理中") print("页面请求处理中")
return render(request, 'index.html') return render(request, 'index.html')
def blog(request, id):
return HttpResponse("id是" + str(id) + "的博客页面")
def blog2(request, year, month, day, id):
return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + '/' + ' id是' + str(id) + "的博客页面")