Django学习:添加文章页面(10)
2017-02-23 20:02
429 查看
博客中添加文章页面及响应
在template中添加add_article.html页面 (form label input)请求方法使用post
这个页面涉及到了两个响应函数 1)显示页面的响应函数 2)表单提交的响应函数
显示页面的响应函数
views.py
urls.py
打开服务器 localhost:8000/blog/edit
表单提交的响应函数
在表单里填写的内容,浏览器通过HTTP请求传递到后台的时候,这些数据都会被写在请求中,因此后台代码接受表单数据的过程实际上就是接受HTTP请求中夹带数据的过程
编辑响应函数
使用 request.POST[' 参数名 '] 获取表单数据 (通过HTTP请求,传递的数据就放在request里,HTTP请求分为post,get等方法,request对不同方法创建了字典,用于存储数据,request.POST里面的键值对就是前端的数据)
取得数据之后,要放入数据库中(models类):
models.Article.objects.create(title,content)创建对象
views.py(return响应后的页面)
urls.py
add_article.html中表单form的action添加
打开服务器 localhost:8000/blog/edit
在表格中输入后,点击提交,页面会出现
禁止访问(403)
CSRF验证失败,响应中断
这是安全性问题,如果用post提交表单,要在form中加入 {% csrf_token %} 用于防范csrf(跨站请求伪造)
index.html中新文章加上url
打开服务器 localhost:8000/blog, 点击添加新文章,完成文章标题和内容的填写,点击提交
在template中添加add_article.html页面 (form label input)请求方法使用post
这个页面涉及到了两个响应函数 1)显示页面的响应函数 2)表单提交的响应函数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>edit article</title> </head> <body> <form action=" " method="post"> <label>文章标题 <input type="text" name="title" /> </label> <p></p> <label>文章内容 <input type="text" name="content" /> </label> <p></p> <input type="submit" value="提交"> </form> </body> </html>
显示页面的响应函数
views.py
def add_page(request): return render(request, 'blog/add_article.html')
urls.py
urlpatterns = [ url(r'^$', views.index), url(r'^article/(?P<article_id>[0-9]+)$', views.page, name='page'), url(r'^edit/$', views.add_page, name='add_page'), ]
打开服务器 localhost:8000/blog/edit
表单提交的响应函数
在表单里填写的内容,浏览器通过HTTP请求传递到后台的时候,这些数据都会被写在请求中,因此后台代码接受表单数据的过程实际上就是接受HTTP请求中夹带数据的过程
编辑响应函数
使用 request.POST[' 参数名 '] 获取表单数据 (通过HTTP请求,传递的数据就放在request里,HTTP请求分为post,get等方法,request对不同方法创建了字典,用于存储数据,request.POST里面的键值对就是前端的数据)
取得数据之后,要放入数据库中(models类):
models.Article.objects.create(title,content)创建对象
views.py(return响应后的页面)
def edit_action(request): title = request.POST.get('title','TITLE') content = request.POST.get('content','CONTENT') models.Article.objects.create(title=title,content=content) arts = models.Article.objects.all() return render(request, 'blog/index.html', {'articles': arts})
urls.py
urlpatterns = [ url(r'^$', views.index), url(r'^article/(?P<article_id>[0-9]+)$', views.page, name='page'), url(r'^edit/$', views.add_page, name='add_page'), url(r'^edit/action$', views.edit_action, name='edit_action'), ]
add_article.html中表单form的action添加
<form action="{% url 'blog:edit_action' %}" method="post">
打开服务器 localhost:8000/blog/edit
在表格中输入后,点击提交,页面会出现
禁止访问(403)
CSRF验证失败,响应中断
这是安全性问题,如果用post提交表单,要在form中加入 {% csrf_token %} 用于防范csrf(跨站请求伪造)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>edit article</title> </head> <body> <form action="{% url 'blog:edit_action' %}" method="post"> {% csrf_token %} <label>文章标题 <input type="text" name="title" /> </label> <p></p> <label>文章内容 <input type="text" name="content" /> </label> <p></p> <input type="submit" value="提交"> </form> </body> </html>
index.html中新文章加上url
<body> <h1><a href="#">我的博客</a> </h1> <h3><a href="{% url 'blog:add_page' %}">添加新文章</a> </h3> {% for wz in articles %} <a href="{% url 'blog:page' wz.pk %}">{{ wz.title }}</a> <br/> {% endfor %} </body>
打开服务器 localhost:8000/blog, 点击添加新文章,完成文章标题和内容的填写,点击提交
相关文章推荐
- Django学习(七)---添加新文章页面
- Django学习(六)---博客文章页面的超链接设置
- Django学习笔记2 模板继承、添加页面
- Django学习笔记三:页面侧边栏最新文章,文章归档,分类,标签云的实现
- Django学习:博客文章页面(超链接)(9)
- Django 开发学习笔记(8)- 开发表单页面,让用户添加内容
- Django学习(八)---修改文章和添加文章
- Django学习:博客文章页面(8)
- Django学习:修改文章和添加文章(11)
- (转)Django ====> 实战学习篇十 让页面联动起来
- ASP.NET MVC 学习4、Controller中添加SearchIndex页面,实现简单的查询功能
- 深入学习Django源码基础10 - Django中http模块
- 一步步学习SPD2010--第三章节--处理列表和库(10)----添加自定义操作
- 一步步学习SPD2010--第三章节--处理列表和库(10)----添加自定义操作
- [django]添加自己的models到管理页面
- 一步步学习微软InfoPath2010和SP2010--第十三章节--SharePoint视图和仪表板(10)--仪表板页面和内容查询Web部件
- 页面每次添加都显示最后一次访问记录spring scope="prototype" 学习笔记
- python django 添加前台页面
- jquery学习之1.22-小练习5-实现添加页面上填写信息到当前页面表格中
- Microsoft Dynamics CRM 4.0 文章页面 添加自定义按钮