URL中的参数

视图

book.views

  1. from django.shortcuts import render
  2. from django.http import HttpResponse
  3. # http://127.0.0.1:8000/book
  4. def book(request):
  5. return HttpResponse("图书首页")
  6. # http://127.0.0.1:8000/book/2
  7. def book_id(request, book_id):
  8. return HttpResponse("图书的id %s" % (book_id))
  9. # http://127.0.0.1:8000/book_detail/2
  10. def book_detail(request, book_id, cate_id):
  11. return HttpResponse("图书的id %s, 分类的id %s" % (book_id, cate_id))
  12. # http://127.0.0.1:8000/book_list/?id=2
  13. # http://127.0.0.1:8000/book_list/?id=2&cid=2
  14. def book_list(request):
  15. # print(type(request))
  16. book_id = request.GET.get('id')
  17. cate_id = request.GET.get('cid')
  18. # cate_id = request.GET['cid']
  19. return HttpResponse("图书的id %s 分类的id %s " % (book_id, cate_id))

路由

主路由

  1. from django.contrib import admin
  2. from django.urls import path, include
  3. from book import views as bviews
  4. from django.http import HttpResponse
  5. def index(request):
  6. return HttpResponse("Hello Django")
  7. urlpatterns = [
  8. path('admin/', admin.site.urls),
  9. path('', index),
  10. path('book/', bviews.book),
  11. path('book/<book_id>', bviews.book_id),
  12. path('book_detail/<book_id>/<cate_id>', bviews.book_detail),
  13. path("book_list/", bviews.book_list),
  14. ]

image.png

总结

参数有2种方式传入:
1.url中,传出方式在函数参数
image.png
2.问号后,传出方式在request.GET.get
此种方式要传递多个参数,可用符号&
image.png

URL模块化

视图

book.urls

  1. from django.urls import path
  2. from . import views
  3. urlpatterns = [
  4. path('', views.book),
  5. path('<book_id>', views.book_id),
  6. path('book_detail/<book_id>/<cate_id>', views.book_detail),
  7. path("book_list/", views.book_list),
  8. ]

主路由

主urls

  1. from django.contrib import admin
  2. from django.urls import path, include
  3. from django.http import HttpResponse
  4. def index(request):
  5. return HttpResponse("Hello Django")
  6. urlpatterns = [
  7. path('admin/', admin.site.urls),
  8. path('', index),
  9. # 子模块
  10. path("book/", include("book.urls")),
  11. ]

访问方式

image.png

【Django内置转换器】

  1. int
  2. str
  3. uuid
  4. path

修改路由

image.png

打印参数类型

image.png

url命名与反转

url命名的好处:解耦(url可改动,view可不改动)

1.基本

1.1创建多个app

django-admin startapp cms
django-admin startapp front

1.2配置cms

视图

  1. from django.shortcuts import render
  2. from django.http import HttpResponse
  3. # http://127.0.0.1:8000/cms/
  4. def index(request):
  5. return HttpResponse("后台首页")
  6. # http://127.0.0.1:8000/cms/login/
  7. def login(request):
  8. return HttpResponse("后台登录页面")

路由

  1. from django.urls import path
  2. from . import views
  3. # 应用命名空间
  4. app_name = "cms"
  5. urlpatterns = [
  6. path("", views.index, name="index"),
  7. path("login/", views.login, name='login'),
  8. ]

主路由

  1. urlpatterns = [
  2. # 子模块
  3. path("cms/", include("cms.urls")),
  4. ]

配置是否成功

image.png

1.3.配置front

视图

  1. from django.shortcuts import render, redirect, reverse
  2. from django.http import HttpResponse
  3. def index(request):
  4. username = request.GET.get("username")
  5. if username:
  6. # http://127.0.0.1:8000/front?username='ecithy'
  7. return HttpResponse("前台首页")
  8. else:
  9. # 重定向
  10. # return redirect('singin/')
  11. # 跳转到http://127.0.0.1:8000/front/signin
  12. return redirect(reverse('front:login'))
  13. # url反转 传参
  14. # return redirect(reverse('front:article', kwargs={"article_id": 2}))
  15. # return redirect(reverse('front:login') + "?name=juran")
  16. # http://127.0.0.1:8000/front/signin/
  17. def login(request):
  18. name = request.GET.get('name')
  19. return HttpResponse("前台登录页面 %s" % name)

路由

  1. from django.urls import path
  2. from . import views
  3. # 应用命名空间
  4. app_name = "front"
  5. urlpatterns = [
  6. # name 给URL起了一个名字
  7. path("", views.index, name='index'),
  8. path("signin/", views.login, name="login"),
  9. ]

主路由

  1. urlpatterns = [
  2. path("front/", include("front.urls")),
  3. ]

配置是否成功

image.png

1.4.URL反转传参

front.urls

  1. path("article/<article_id>", views.article, name="article"),

参数类型1

front.views

  1. def index(request):
  2. username = request.GET.get("username")
  3. if username:
  4. # http://127.0.0.1:8000/front?username='ecithy'
  5. return HttpResponse("前台首页")
  6. else:
  7. # url反转 传参
  8. # 访问 http://127.0.0.1:8000/front
  9. # 跳转到 http://127.0.0.1:8000/front/article/2
  10. return redirect(reverse('front:article', kwargs={"article_id": 2}))
  11. def article(request, article_id):
  12. return HttpResponse("文章的id %s" % article_id)

参数类型2

front.views

  1. def index(request):
  2. username = request.GET.get("username")
  3. if username:
  4. # http://127.0.0.1:8000/front?username='ecithy'
  5. return HttpResponse("前台首页")
  6. else:
  7. # url反转 传参
  8. # 访问 http://127.0.0.1:8000/front
  9. # 跳转到 http://127.0.0.1:8000/front/signin/?name=ecithy
  10. return redirect(reverse('front:login') + "?name=ecithy")
  11. def article(request, article_id):
  12. return HttpResponse("文章的id %s" % article_id)

2.namespace

一般不用,作为了解

主路由

image.png

视图

  1. from django.shortcuts import render
  2. from django.http import HttpResponse
  3. def index(request):
  4. current_namespace = request.resolver_match.namespace
  5. print(current_namespace)
  6. return HttpResponse("后台首页")
  7. def login(request):
  8. return HttpResponse("后台登录页面")

浏览器访问效果

先后访问
http://127.0.0.1:8000/cms/
http://127.0.0.1:8000/cms1/
http://127.0.0.1:8000/cms2/
image.png

3.默认参数

新建app

django-admin startapp article

配置app

视图

article.views

  1. from django.urls import path, re_path
  2. from django.conf.urls import url
  3. from . import views
  4. urlpatterns = [
  5. # path("", views.index),
  6. # 默认参数
  7. path("page/", views.page),
  8. # 传的参数
  9. path("page/<page_num>", views.page),
  10. ]

路由

article.urls

  1. from django.urls import path, re_path
  2. from django.conf.urls import url
  3. from . import views
  4. urlpatterns = [
  5. # path("", views.index),
  6. # 默认参数
  7. path("page/", views.page),
  8. # 传的参数
  9. path("page/<page_num>", views.page),
  10. ]

主路由

  1. path("article/", include("article.urls")),

配置是否成功

image.png
image.png
image.png

4.url/re_path函数

作用

URL支持正则语法

原理

image.png
从django源码可知,url与re_path函数一样,可能是搞版本为了兼容,或者改名重新封装了原函数。

视图

article.views

  1. def article_year(request, year):
  2. return HttpResponse("年份是 %s " % year)
  3. def article_month(request, month):
  4. return HttpResponse("月份是 %s " % month)

路由

article.urls

  1. re_path(r"article_year/(?P<year>\d{4})$", views.article_year),
  2. re_path(r"article_year/(?P<month>\d{2})", views.article_month)