Django之urlconf路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from django.contrib import admin
from django.urls import path

# 路由层URLconf
# 首先导入模块
from django.urls import re_path
# 然后导入项目下的视图
from app01 import views
# 配置路由
# 不含正则表达式 默认带开头结尾^$ 用path
# 含正则表达式 用re_path

# urlpatterns = [
# re_path(r'^$', views.index),
# re_path('admin/', admin.site.urls),
# re_path(r'^articles/2018/$', views.special_case_2018),
# re_path(r'^articles/([0-9]{4})/$', views.year_achive),
# re_path(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_achive),
# re_path(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
# ]

# 若需要从URL中获取值,只需要在他周围添加一对圆括号() 比如([0-9]{4})、([0-9]{2})
# 不需要在最前面添加反斜杠/ 因为每个URL都有 正确的是 ^articles... 而不是^/articles...
# 每个正在表达式前面的'r' 告诉python不转义

# 请求URL articles/2018/11/ 请求将匹配列表第五个模式
# django 将调用视图函数 views.month_achive(request, '2018', '11')
# 请求URL articles/2018/11/123/ 请求将匹配列表第六个模式
# django 将调用视图函数 views.article_detail(request, '2018', '11', '123')

# 命名分组
# urlpatterns = [
# re_path(r'^$', views.index),
# re_path('admin/', admin.site.urls),
# re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_achive),
# re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_achive),
# re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<id>[0-9]+)/$', views.article_detail),
# ]
# 获取的值作为关键字参数而不是位置参数传递给试图函数
# URLconf会更更加清晰而不会产生参数顺序问题
# 请求URL articles/2018/11/ 请求将匹配列表第四个模式 django
# 将调用视图函数 views.month_achive(request, year='2018', month='11')
# 请求URL articles/2018/11/123/ 请求将匹配列表第五个模式
# django 将调用视图函数 views.article_detail(request, year='2018', month='11', id='123')

# 分发
# 项目下url配置一个项目入口路由
# 项目下url再配置具体路由
# 首先导入模块
from django.urls import include
# 配置路由
urlpatterns = [
re_path(r'^$', views.index),
# 配置分发
re_path(r'^app01/', include('app01.urls', namespace='app01')),
re_path(r'^app02/', include('app02.urls', namespace='app02')),
re_path('^admin/', admin.site.urls),
]

# 命名空间namespace='app01'
# 注意对应项目下urls文件需要配置app_name = 'app01'

app01/urls

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/11/20 14:08
# @Author : Ropon
# @File : urls.py
from django.urls import re_path
from . import views
app_name = 'app01'
urlpatterns = [
re_path(r'^index/', views.index, name='index'),
re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_achive, name='news_year_achive'),
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_achive, name='news_month_achive'),
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<id>[0-9]+)/$', views.article_detail),
]

# url反向解析
# 根url 配置re_path(r'^app01/', include('app01.urls', namespace='app01')),
# 项目url 配置re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_achive, name='news_year_achive'),
# 注意 urls.py文件中必须声明app_name

# 模板中使用{% url 'app01:news_year_achive' year=year %}
# 如果存在参数传值
# 关键字参数值 在后面加key=value 多个参数使用空格隔开 如year=2018 month=11 {% url 'app01:news_year_achive' year=year %}
# 位置参数 在后面加参数值 多个参数使用空格隔开 2018 11 {% url 'app01:news_year_achive' 2018 %}

# 在视图及python中使用
# def year_achive(request, year=None):
# month = 12
# return HttpResponseRedirect(reverse('app01:news_month_achive', kwargs = {'year':year, 'month':month}))
# 如果存在参数传值
# 关键字参数值 kwargs={'key':value, key1:value1}
# 位置参数 args=() 或 args=[]

app01/view

from django.shortcuts import render

# 导入HttpResponse模块
from django.http import HttpResponse
# Create your views here.
# 编写视图函数 第一个参数始终是request
# 而且一定要返回一个HttpResponse实例化对象

# 反向解析
# 导入reverse、HttpResponseRedirect模块
from django.urls import reverse
from django.http import HttpResponseRedirect

def index(request):
return HttpResponse('index1')

def year_achive(request, year=None):
# return HttpResponse('year '+year)
# year_list = ['2015' ,'2016', '2017', '2018']
month = 12
# return render(request, 'app01/year_achive.html', {"year": year, 'year_list': year_list})
return HttpResponseRedirect(reverse('app01:news_month_achive', kwargs = {'year':year, 'month':month}))


def news_year_achive(request, year=None):
return render(request, 'app01/year_achive.html',{"year": year})

def month_achive(request,year=None, month=None):
return HttpResponse('year1 '+year+' month '+month)

def article_detail(request, year=None, month=None, id=None):
return HttpResponse('year2 '+year+' month1 '+month+' id '+id)

template/app01/year_achive.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>
<a href="{% url 'app01:news_year_achive' year=year %}">单个 反向解析URL地址:查看{{ year }}年的文章</a>
</p>
<p>
<ul>
{% for yearvar in year_list %}
<li><a href="{% url 'app01:news_year_achive' year=yearvar %}">循环 反向解析URL地址:查看{{ yearvar }}年的文章</a></li>
{% endfor %}
</ul>
</p>
</body>
</html>