码迷,mamicode.com
首页 > 其他好文 > 详细

django3.02模板中的超链接配置

时间:2020-02-04 14:02:45      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:函数   object   color   超链接   配置   title   模板   meta   set   

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}}">{{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>

 

django3.02模板中的超链接配置

标签:函数   object   color   超链接   配置   title   模板   meta   set   

原文地址:https://www.cnblogs.com/manhton/p/12258970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!