urls
容易搞混的概念,域名,主机名,路由
网站模块多主机应用
不同模块解析不同的服务器ip地址
网页模块多路径应用
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(‘’, app01views.index),
path(‘movie/’, app01views.movie),
path(‘movie/int:movie_id/’, app01views.movie_detail)
]
url传值
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(‘’, app01views.index),
path(‘movie/’, app01views.movie),
path(‘movie/int:movie_id/’, app01views.movie_detail)
]
使用url中查询字符串传值
def movie_detail(request):
movie_id = request.GET.get(“movieid”)
city = request.GET.get(“city”)
return HttpResponse(“正在播放编号为:%s的电影, 城市:%s” % (movie_id,city))
path(‘movie/detail/’, app01views.movie_detail),
测试访问链接
http://127.0.0.1:8000/movie/detail/?movieid=1&city=北京
re_path
re_path(‘movie/detail/(?P<movie_id>[9][5]\d{4})/(?P[0-4])’,app01views.movie_detail)
def movie_detail(request, movie_id, type):
type_name = [“喜剧片”, “动作片”, “爱情片”, ‘纪录片’, ‘历史剧’]
return HttpResponse(“正在播放电影编号为:%s的电影! \n 播放的电影类型:%s” %
(movie_id,type_name[int(type)]))