Django入门与实践:七、博客文章页开发
发布日期:2021-05-07 01:04:25 浏览次数:12 分类:技术文章

本文共 910 字,大约阅读时间需要 3 分钟。

页面内容:

标题

文章内容

修改文章按钮(超链接)

myblog/blog/views.py:

from django.shortcuts import renderfrom django.http import HttpResponsefrom . import models# Create your views here.def index(request):    #return HttpResponse("Hello world!")    #return render(request, 'index.html', {'hello': 'hello, blog!'})    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})

myblog/blog/templates/blog/article_page.html:

    
article page

{ {article.title}}

{ {article.content}}

修改

myblog/blog/urls.py:

from django.urls import path, re_pathfrom . import viewsurlpatterns = [    path('index/', views.index),    re_path('^article/(?P
[0-9]+)/$', views.article_page),]

注:正则中的组名必须和参数名一致!

浏览器中输入:即可访问。

上一篇:[AH2017/HNOI2017]抛硬币
下一篇:[CF932E]Team Work

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年03月12日 21时17分12秒