本文共 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),]
注:正则中的组名必须和参数名一致!
浏览器中输入:即可访问。
转载地址:http://rctq.baihongyu.com/