欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

django3.02模板中的超链接配置实例代码

时间:2021-02-26 10:52:49|栏目:Python代码|点击:

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>

上一篇:Keras之fit_generator与train_on_batch用法

栏    目:Python代码

下一篇:Python中的面向对象编程详解(下)

本文标题:django3.02模板中的超链接配置实例代码

本文地址:http://www.codeinn.net/misctech/70324.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有