URL中的参数
视图
book.views
from django.shortcuts import render
from django.http import HttpResponse
# http://127.0.0.1:8000/book
def book(request):
return HttpResponse("图书首页")
# http://127.0.0.1:8000/book/2
def book_id(request, book_id):
return HttpResponse("图书的id %s" % (book_id))
# http://127.0.0.1:8000/book_detail/2
def book_detail(request, book_id, cate_id):
return HttpResponse("图书的id %s, 分类的id %s" % (book_id, cate_id))
# http://127.0.0.1:8000/book_list/?id=2
# http://127.0.0.1:8000/book_list/?id=2&cid=2
def book_list(request):
# print(type(request))
book_id = request.GET.get('id')
cate_id = request.GET.get('cid')
# cate_id = request.GET['cid']
return HttpResponse("图书的id %s 分类的id %s " % (book_id, cate_id))
路由
主路由
from django.contrib import admin
from django.urls import path, include
from book import views as bviews
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello Django")
urlpatterns = [
path('admin/', admin.site.urls),
path('', index),
path('book/', bviews.book),
path('book/<book_id>', bviews.book_id),
path('book_detail/<book_id>/<cate_id>', bviews.book_detail),
path("book_list/", bviews.book_list),
]
总结
参数有2种方式传入:
1.url中,传出方式在函数参数
2.问号后,传出方式在request.GET.get
此种方式要传递多个参数,可用符号&
URL模块化
视图
book.urls
from django.urls import path
from . import views
urlpatterns = [
path('', views.book),
path('<book_id>', views.book_id),
path('book_detail/<book_id>/<cate_id>', views.book_detail),
path("book_list/", views.book_list),
]
主路由
主urls
from django.contrib import admin
from django.urls import path, include
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello Django")
urlpatterns = [
path('admin/', admin.site.urls),
path('', index),
# 子模块
path("book/", include("book.urls")),
]
访问方式
【Django内置转换器】
int
str
uuid
path
修改路由
打印参数类型
url命名与反转
1.基本
1.1创建多个app
django-admin startapp cms
django-admin startapp front
1.2配置cms
视图
from django.shortcuts import render
from django.http import HttpResponse
# http://127.0.0.1:8000/cms/
def index(request):
return HttpResponse("后台首页")
# http://127.0.0.1:8000/cms/login/
def login(request):
return HttpResponse("后台登录页面")
路由
from django.urls import path
from . import views
# 应用命名空间
app_name = "cms"
urlpatterns = [
path("", views.index, name="index"),
path("login/", views.login, name='login'),
]
主路由
urlpatterns = [
# 子模块
path("cms/", include("cms.urls")),
]
配置是否成功
1.3.配置front
视图
from django.shortcuts import render, redirect, reverse
from django.http import HttpResponse
def index(request):
username = request.GET.get("username")
if username:
# http://127.0.0.1:8000/front?username='ecithy'
return HttpResponse("前台首页")
else:
# 重定向
# return redirect('singin/')
# 跳转到http://127.0.0.1:8000/front/signin
return redirect(reverse('front:login'))
# url反转 传参
# return redirect(reverse('front:article', kwargs={"article_id": 2}))
# return redirect(reverse('front:login') + "?name=juran")
# http://127.0.0.1:8000/front/signin/
def login(request):
name = request.GET.get('name')
return HttpResponse("前台登录页面 %s" % name)
路由
from django.urls import path
from . import views
# 应用命名空间
app_name = "front"
urlpatterns = [
# name 给URL起了一个名字
path("", views.index, name='index'),
path("signin/", views.login, name="login"),
]
主路由
urlpatterns = [
path("front/", include("front.urls")),
]
配置是否成功
1.4.URL反转传参
front.urls
path("article/<article_id>", views.article, name="article"),
参数类型1
front.views
def index(request):
username = request.GET.get("username")
if username:
# http://127.0.0.1:8000/front?username='ecithy'
return HttpResponse("前台首页")
else:
# url反转 传参
# 访问 http://127.0.0.1:8000/front
# 跳转到 http://127.0.0.1:8000/front/article/2
return redirect(reverse('front:article', kwargs={"article_id": 2}))
def article(request, article_id):
return HttpResponse("文章的id %s" % article_id)
参数类型2
front.views
def index(request):
username = request.GET.get("username")
if username:
# http://127.0.0.1:8000/front?username='ecithy'
return HttpResponse("前台首页")
else:
# url反转 传参
# 访问 http://127.0.0.1:8000/front
# 跳转到 http://127.0.0.1:8000/front/signin/?name=ecithy
return redirect(reverse('front:login') + "?name=ecithy")
def article(request, article_id):
return HttpResponse("文章的id %s" % article_id)
2.namespace
主路由
视图
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
current_namespace = request.resolver_match.namespace
print(current_namespace)
return HttpResponse("后台首页")
def login(request):
return HttpResponse("后台登录页面")
浏览器访问效果
先后访问
http://127.0.0.1:8000/cms/
http://127.0.0.1:8000/cms1/
http://127.0.0.1:8000/cms2/
3.默认参数
新建app
配置app
视图
article.views
from django.urls import path, re_path
from django.conf.urls import url
from . import views
urlpatterns = [
# path("", views.index),
# 默认参数
path("page/", views.page),
# 传的参数
path("page/<page_num>", views.page),
]
路由
article.urls
from django.urls import path, re_path
from django.conf.urls import url
from . import views
urlpatterns = [
# path("", views.index),
# 默认参数
path("page/", views.page),
# 传的参数
path("page/<page_num>", views.page),
]
主路由
path("article/", include("article.urls")),
配置是否成功
4.url/re_path函数
作用
原理
从django源码可知,url与re_path函数一样,可能是搞版本为了兼容,或者改名重新封装了原函数。
视图
article.views
def article_year(request, year):
return HttpResponse("年份是 %s " % year)
def article_month(request, month):
return HttpResponse("月份是 %s " % month)
路由
article.urls
re_path(r"article_year/(?P<year>\d{4})$", views.article_year),
re_path(r"article_year/(?P<month>\d{2})", views.article_month)