mysite
mysite
- urls.py
from app01 import views
# 路由系统
urlpatterns = [
url(r'^index/(\d+)/', views.index), # 分页函数
url(r'^detail/(\d+)/', views.detail), # 查看详情
]
app01
- views.py
USER_LIST = []
# 生成多行数据
for item in range(99):
temp = {"id": item, "username": "alex"+str(item), "email": "alex"+str(item)+"@xxx.com"}
USER_LIST.append(temp)
# 分页函数
def index(request, page):
page = int(page) # 接收的参数是个字符串,需要转换成整数
start = (page-1)*10 # 分页开头索引
end = page*10 #分页结束索引
user_list = USER_LIST[start:end] # 取分页当前页的内容
return render(request, 'index.html', {"user_list": user_list})
# 查看详情
def detail(request, nid):
nid = int(nid)
current_detail = USER_LIST[nid] #获取当前信息的详细内容
return render(request, 'detail.html', {'current_detail': current_detail})
templates
- index.html
<table border="1" style="border-collapse: collapse">
<tr>
<td>ID</td>
<td>用户名</td>
<td>详情</td>
</tr>
{% for item in user_list %}
<tr>
<td>{{ item.id }}</td>
<td>{{ item.username }}</td>
<td><a href="/detail/{{ item.id }}" target="_blank">查看详情</a></td>
# 点击a标签,跳转到detail页,并将该条数据的id当做参数传入
</tr>
{% endfor %}
</table>
- detail.html
<ul>
<li>{{ current_detail.id }}</li>
<li>{{ current_detail.username }}</li>
<li>{{ current_detail.email }}</li>
</ul>