目录
原视频教程链接
博客页面设计
- 博客主页面
- 博客文章内容页面
- 博客撰写页面
博客主页面开发
页面内容
- 文章标题列表,超链接
- 发表博客按钮(超链接)
代码编写
1、修改blog/views.py
中的index
函数:
def index(request):
articles = models.Article.objects.all() # 获取所有文章对象列表
return render(request, 'blog/index.html', {'articles': articles}) # 传递所有文章,作为参数
2、修改templates/blog
下的index.html
:
添加:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blog</title>
</head>
<body>
<h1>
<a href="{#这里先空着#}">新文章</a>
</h1>
{#遍历articles中的所有文章,生成对应链接#}
{% for article in articles %}
<a href="{#这里先空着#}">{{ article.title }}</a>
<br>
{% endfor %}
</body>
</html>
博客文章页面开发
页面内容
- 标题
- 文章内容
- 修改文章(超链接)
代码编写
1、在blog/views.py
中添加article_page
函数:
def article_page(request, article_id): # 传入文章id
article = models.Article.objects.get(pk=article_id) # 根据文章id获取文章对象
return render(request, 'blog/article_page.html', {'article': article}) # 传递文章对象
2、在blog/urls.py
中添加:
url(r'^article/(?P<article_id>[0-9]+)$', views.article_page),
# 获取article_id值,并传递给article_page函数
3、在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>
Django模板中的超链接配置
超链接配置使用Django的命名空间
命名空间-xiong的博客
超链接目标地址
href
后面是目标地址- 模板中可以用
"{% url 'app_name:url_name' param%}"
作为地址 - 其中
app_name
和url_name
都可以在url
函数中设置
配置app_name
在myblog/myblog/urls.py
下修改:
url(r'^blog/', include('blog.urls', namespace='blog')),
配置url_name
在blog/urls.py
下修改:
url(r'^article/(?P<article_id>[0-9]+)$', views.article_page, name='article_page'),
在模板中使用
修改index.html
<a href="{% url 'blog:article_page' article.id %}">{{ article.title }}</a>
博客文章撰写页面
页面内容
- 标题编辑栏
- 文章内容编写区域
- 提交按钮
小技巧
背景
- html表单具有隐藏属性
- 文章
id
从1开始
使用隐藏的input记录article_id
<input type="hidden" name="article_id" value="{{ article.id }}">
根据前端传来的article_id决定edit页面的显示方式
{% if article %}{# 有article传入,显示出原文章内容 #}
<input type="hidden" name="article_id" value="{{ article.id }}">{# 隐藏表单存放article.id #}
<lable>文章标题
<input type="text" name="title" value="{{ article.title }}">
</lable>
<br>
<lable>文章内容
<input type="text" name="content" value="{{ article.content }}">
</lable>
<br>
<input type="submit" value="提交">
</form>
{% else %}{# 没有article传入 #}
<input type="hidden" name="article_id" value="0">{# 隐藏表单存放article.id,新建文章设置id为0#}
<lable>文章标题
<input type="text" name="title">
</lable>
<br>
<lable>文章内容
<input type="text" name="content">
</lable>
<br>
<input type="submit" value="提交">
</form>
{% endif %}
根据article_id决定文章是新建还是修改
def edit_action(request):
# 获取表单数据
title = request.POST.get('title', 'TITLE')
content = request.POST.get('content', 'CONTENT')
article_id = request.POST.get('article_id', '0')
if article_id == '0': # 创建新文章
models.Article.objects.create(title=title, content=content)
return HttpResponseRedirect('/blog/index')
# 修改文章
article = models.Article.objects.get(pk=article_id)
article.title = title
article.content = content
article.save()
return HttpResponseRedirect('/blog/index')
代码编写
1、创建edit_page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Edit Page</title>
</head>
<body>
<form action="{% url 'blog:edit_action' %}" method="post">
{% csrf_token %}
{% if article %}{# 有article传入,显示出原文章内容 #}
<input type="hidden" name="article_id" value="{{ article.id }}">{# 隐藏表单存放article.id #}
<lable>文章标题
<input type="text" name="title" value="{{ article.title }}">
</lable>
<br>
<lable>文章内容
<input type="text" name="content" value="{{ article.content }}">
</lable>
<br>
<input type="submit" value="提交">
</form>
{% else %}{# 没有article传入 #}
<input type="hidden" name="article_id" value="0">{# 隐藏表单存放article.id,新建文章设置id为0#}
<lable>文章标题
<input type="text" name="title">
</lable>
<br>
<lable>文章内容
<input type="text" name="content">
</lable>
<br>
<input type="submit" value="提交">
</form>
{% endif %}
</body>
</html>
2、在blog/views.py中添加:
def edit_page(request, article_id):
if str(article_id) == '0': # 如果article_id为0,即新建文章,不传参数
return render(request, 'blog/edit_page.html')
article = models.Article.objects.get(pk=article_id) # 否则查找文章对象并传递
return render(request, 'blog/edit_page.html', {'article': article})
def edit_action(request):
# 获取表单数据
title = request.POST.get('title', 'TITLE')
content = request.POST.get('content', 'CONTENT')
article_id = request.POST.get('article_id', '0')
if article_id == '0': # 创建新文章
models.Article.objects.create(title=title, content=content)
return HttpResponseRedirect('/blog/index')
# 修改文章
article = models.Article.objects.get(pk=article_id)
article.title = title
article.content = content
article.save()
return HttpResponseRedirect('/blog/index')
3、在blog/urls.py添加:
url(r'^edit/(?P<article_id>[0-9]+)$', views.edit_page, name='edit_page'),
url(r'^edit/action$', views.edit_action, name='edit_action'),
4、将之前空着的html中href属性填写好