1.在myblog中的urls.py中

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')
]

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}}" rel="external nofollow" >{{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>

以上代码大家可以在本地测试下,如果有任何补充可以联系小编。

标签:
django,超链接配置

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“django3.02模板中的超链接配置实例代码”
暂无“django3.02模板中的超链接配置实例代码”评论...