Django博客文章页面开发
一 页面内容
-
标题
-
内容
-
修改文章按钮(超链接)
二 URL传递参数
-
参数写在响应函数中request后,可以有默认值
-
URL正则表达式:r'^article/(?P<article_id>[0-9]+)$
-
URL正则中的组名必须和参数名一致
三 实战
1 后端 views.py
from django.shortcuts import render
from django.http import HttpResponse
from . import models
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})
2 前端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>
3 urls.py
from django.conf.urls import url,include
from . import views
urlpatterns = [
url(r'^index/$', views.index),
url(r'^article/(?P<article_id>[0-9]+)$', views.article_page),
]
4 运行结果