接上一章继续。
编辑页面已经实现接下来是编辑响应的函数部分。 使用request.POST[‘参数名’]来获取表单数据 models.Article.objects.create(title, content)来创建对象
具体实现如下。 首先增加一个html文件为edit_action.html 然后views增加一个响应,在这里我们选择编辑完成返回主页
def edit_action(request): title = request.POST.get('title', 'page_title') content = request.POST.get('content', 'page_content') models.Article.objects.create(title=title, content=content) //以上是创建新的数据库对象 //下面是获取数据库对象显示(每次都这样获取确实效率很低 //可以用redis缓存一下) articles = models.Article.objects.all() return render(request, 'myblog/myblog.html', {'articles':articles})随之urls增加
url(r'^edit/action$', views.edit_action, name='edit_action'),然后修改一下html文件的表单响应
<form action="{% url 'blog:edit_action' %}" method="post"> {% csrf_token %}特别注意要添加{% csrf_token %},因为Django安全性要求很高,否则会出现错误。 最终效果如下 图1 增加新文章
新文章没有标题和内容,而修改文章则需要含义原标题和内容,也就是修改文章页面有文章对象。 所有我们可以根据文章的ID来获取文章对象,id是从1开始,我门设置当id=0时是新文章页面,否则是带有文章对象的编辑页面 接下来来编写代码 首先是修改响应代码views:
def edit_page(request, article_id): #0代表新文章页面(没有文章对象传递给前端) if str(article_id) == '0': return render(request, 'myblog/edit_page.html') #否在id代表了文章对象的id article = models.Article.objects.get(pk=article_id) return render(request, 'myblog/edit_page.html', {'article':article})上面修改的views给edit_page添加了一个参数,所以urls修改让其能接受一个参数。
urlpatterns = [ url(r'^blog/$', views.myblog), url(r'^article/(?P<article_id>[0-9]+)$', views.article_page, name='article_page'), url(r'^edit/(?P<article_id>[0-9]+)$', views.edit_page, name='edit_page'), url(r'^edit/action$', views.edit_action, name='edit_action'), ]然后修改前端代码 让主页的新建文章带有一个0的参数,表明是新文章
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>博客主页</title> </head> <body> {% for article in articles %} <a href="{% url 'myblog:article_page' article.id %}">{{ article.title }}</a> <br/> {% endfor %} <h3> <!--增加新文章页面会给edit_page函数传递默认参数 0 --> <a href="{% url 'myblog:edit_page' 0 %}">增加新文章</a> </h3> </body> </html>最后修改编辑页面edit_page.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>编辑</title> </head> <body> <form action="{% url 'myblog:edit_action' %}" method="post"> {% csrf_token %} <!--这个页面提交 会反馈给edit_action响应 在此相当于填充了一个数据对象 然后在edit_action 中获取这个对象 检查id是0还是1 以此来创建新文章 还是原来的文章--> <input type="hidden" name="article_id" value="{{article.id | default:'0'}}"/> <label>博客标题 <input type="text" name="title" value="{{article.title}}" style="width:300px;"/> </label> <br/> <label>博客内容 <input type="text" name="content" value="{{article.content}}" style="width:300px;height:200px"/> </label> <br/> <input type="submit" value="点击提交"> </form> </body> </html>上面完成了页面设置,接下来只需要编辑页面的获取需要根据id是0(新页面没有文章对象)是1带有文章对象,即有数据。 修改views的响应也就是编辑
def edit_action(request): title = request.POST.get('title', 'page_title') content = request.POST.get('content', 'page_content') article_id = request.POST.get('article_id', '0') if article_id == '0': # 创建一个数据库对象(新) 然后返回主页 # 主页需要显示全部的数据库列表 models.Article.objects.create(title=title, content=content) articles = models.Article.objects.all() return render(request, 'myblog/myblog.html', {'articles':articles}) # 获取当前需要编辑的文章对象 然后传递给前端 article = models.Article.objects.get(pk=article_id) article.title = title article.content = content article.save() return render(request, 'myblog/atricle_page.html', {'article':article})查看效果 图2 编辑文章