1.前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>反向解析</title>
</head>
<body>
<h2>这是一个普通路由案例:<a href="/blog/abc/">普通路由</a></h2>
<h2>这是一个有名分组案例:<a href="/blog/{{ art.sub }}/">有名分组</a></h2>
<h2>这是一个反向解析案例:<a href="{% url 'article_url' %}">反向解析</a></h2>
</body>
</html>
2.视图
from .models import Grade, Person
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import redirect, reverse
# 普通路由
def blog(request):
return render(request, 'test.html', context=None)
# 有名分组
def blog(request, num):
if Grade.objects.filter(id=num):
art = Grade.objects.filter(id=num).first()
return render(request, 'test.html', context={'art': art})
# 反向解析
def article(request):
return HttpResponse('你来对了,这是反向解析成功案例')