添加新文章:
在template中添加edit.html页面
<!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 %} <!--安全性问题,用post提交表单时,要在form中加入 {% csrf_token %} 用于防范csrf(跨站请求伪造)-->
<label>文章标题:<br/>
<textarea name="title" cols="80" rows="2" >title</textarea> <!--注意name,后台获取数据的标识-->
</label>
<p></p>
<label>文章内容:<br/>
<textarea name="content" cols="80" rows="50" >content</textarea>
</label>
<p></p>
<input type="submit" value="提交"> <!--提交按钮-->
</form>
</body>
</html>
这个页面涉及到了两个响应函数
(1)显示页面的响应函数
views.py中加入
def edit_page(request): #显示页面的响应函数
return render(request,'edit_page.html')
(2)表单提交的响应函数
views.py中加入
def edit_action(request): #提交表单的响应函数
title=request.POST.get('title', 'TITLE') #获取提交的title,失败则获取到'TITLE'
content = request.POST.get('content', 'CONTENT')
models.Article.objects.create(title=title, content=content) #创建一个新对象(取得的数据,通过models类的操作放入数据库中)
return HttpResponseRedirect("/nic/index/") #提交完成后重定向到index
使用 request.POST[' 参数名 '] 获取表单数据 (通过HTTP请求,传递的数据就放在request里,HTTP请求分为post,get等方法,request对不同方法创建了字典,用于存储数据,request.POST里面的键值对就是前端的数据)
添加edit.html中表单form
<form action="{% url 'blog:edit_action' %}" method="post">
配置URL
index.html中新文章超链接加上url
<a href="{% url 'blog:article_page' article.id %}">{{article.title}}</a>
该app的urls.py中加入
url(r'^edit/$', views.edit_page, name='edit_page'),
url(r'^edit/action/$', views.edit_action, name='edit_action'),
修改文章:从主页点击不同文章的超链接进入文章页面,就是传递了一个id作为参数,然后后台代码根据这个参数从数据库中取出来对应的文章,并把它传递到前端页面。修改文章和添加新文章,都要进入编辑页面,但编辑页面一个内容为空,一个有内容。根据上述思路,可通过id 来区分不同的编辑页面(将添加新文章的编辑页面id设为0)。
修改edit_page
views.py中修改edit_page()函数
def edit_page(request,article_id): #显示页面的响应函数
if str(article_id) == '0': #添加文章时article_id为0,则直接跳转到edit页面
return render(request,'edit_page.html')
article=models.Article.objects.get(pk=article_id) #获取文章传到编辑页面
return render(request, 'edit_page.html', {'article': article})
urls.py中对应url加上article_id
url(r'^edit/(?P<article_id>[0-9]+)$', views.edit_page, name='edit_page'),
article_page.html 添加修改文章的url
<a href="{% url'blog:edit_page'article_id %}">修改文章</a>
index.html 添加新文章url中加设为0
<a href="{% url 'blog:edit_page' 0 %}">添加新文章</a>
文章编辑页面edit_page.html页面加入条件判断,不存在的文章传入value,显示文章内容
{% if article %} <!--若文件存在则将文章内容显示在textarea-->
<label>文章标题:<br/>
<textarea name="title" cols="80" rows="2" >{{ article.title }}</textarea> <!--注意name,后台获取数据的标识-->
</label>
<p></p>
<label>文章内容:<br/>
<textarea name="content" cols="80" rows="50" >{{ article.content}}</textarea>
</label>
{% else %} <!--如果article不存在,显示空白编辑区域-->
<label>文章标题:<br/>
<textarea name="title" cols="80" rows="2" >title</textarea> <!--注意name,后台获取数据的标识-->
</label>
<p></p>
<label>文章内容:<br/>
<textarea name="content" cols="80" rows="50" >content</textarea>
</label>
{% endif %}
修改edit_action
在edit_page.html中 添加提交article_id 表单的语句
修改文章时传递article_id
<input type="hidden" name="article_id" value="{{ article.id }}" <!--修改文章时,将article_id设为该文章的ID-->
新建文章时传递0
<input type="hidden" name="article_id" value="0" <!--新建文章时,将article_id设为0-->
修改views.py中的响应函数
def edit_action(request): #提交表单的响应函数
title=request.POST.get('title', 'TITLE') #获取提交的title,失败则获取到'TITLE'
content = request.POST.get('content', 'CONTENT')
article_id=request.POST.get('article_id','0')
if str(article_id) == '0': #创建新文章对象
models.Article.objects.create(title=title, content=content) #创建一个新对象(取得的数据,通过models类的操作放入数据库中)
return HttpResponseRedirect("/nic/index/") #提交完成后重定向到index
article=models.Article.objects.get(pk=article_id) #获取到该id对应的文章对象
article.title=title
article.content=content
article.save() #修改对象后的保存
return HttpResponseRedirect("/nic/index/") # 提交完成后重定向到index