欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

django3.02模板中的超鏈接配置實例代碼

 更新時間:2020年02月04日 15:22:31   作者:manton  
在本篇文章里小編給大家整理了關(guān)于django3.02模板中的超鏈接配置實例代碼內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。

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的第三個參數(shù)是用來傳遞數(shù)據(jù)到前端的,函數(shù)中支持一個disc參數(shù)(字典類型的數(shù)據(jù))

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>

以上代碼大家可以在本地測試下,如果有任何補充可以聯(lián)系腳本之家小編。

相關(guān)文章

最新評論