django3.02模板中的超链接配置
1.在myblog中的urls.py中(配置全局的页面的响应的url参数)
from django.urls import include from django.conf.urls import url urlpatterns = [ path('blog/',include('blog.urls')), #使前端页面访问的时候能找到应用的地址 ]
2.在blog的urls.py中
from django.urls import path from django.conf.urls import url from . import views urlpatterns = [ path('index',views.index), path('article/<int:article_id>',views.article_page,name='article_page') #在访问article/数字的时候访问的是指定的函数下设置的网页 ]
3.在blog的view.py中
from django.shortcuts import render from django.http import HttpResponse from . import models # Create your views here. def index(request): articles = models.Article.objects.all() return render(request, 'blog/index.html', {'articles': articles}) def article_page(request,article_id): article = models.Article.objects.get(pk=article_id) #把参数传送到前端 return render(request,'blog/article_page.html',{'article':article})#设置的指定的模板 #redner的第三个参数是用来传递数据到前端的,函数中支持一个disc参数(字典类型的数据)
4.在blog/templates/blog/index中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>title</title> </head> <body> <h1><a href="">新文章</a></h1> {% for article in articles %} <a href="/blog/article/{{article.id}}">{{article.title}}</a> #接受后台传送的参数后,在前端显示 <br/> {% endfor %} </body> </html>
5.在blog/templates/blog/article_page.html中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>article page</title> </head> <body> <h1>{{article.title}}</h1> <br/> <h3>{{article.content}}</h3> <br/><br/> <a href="">修改文章</a> </body> </html>

浙公网安备 33010602011771号