您的位置:首页 > 编程语言 > Go语言

Django学习:修改文章和添加文章(11)

2017-02-24 16:31 323 查看
博客页面的修改文章和添加新文章

从主页点击不同文章的超链接进入文章页面,就是传递了一个id作为参数,然后后台代码根据这个参数从数据库中取出来对应的文章,并把它传递到前端页面

修改文章和添加新文章,是要进入编辑页面,但编辑页面一个内容为空,一个有内容

根据上述思路,通过id 来区分不同的编辑页面(添加新文章的编辑页面id设为0即可)

传id到后台的两种方法:1)通过url传递文章id ,添加响应函数的参数  2)把id放在隐藏标签的value里面

利用方法1来修改add_page响应函数

view.py中add_article.html页面的显示页面响应函数中添加article_id参数

如果参数为0,直接返回添加新文章表单页面

不为0,获取数据库中主键为article_id的数据对象,传入前端

def add_page(request, article_id):
if str(article_id) == '0':
return render(request, 'blog/add_article.html')
art = models.Article.objects.get(pk=article_id)
return render(request, 'blog/add_article.html', {'article': art})


urls.py中对应url加上article_id

url(r'^edit/(?P<article_id>[0-9]+)$', views.add_page, name='add_page'),


文章页面page.html 添加修改文章的url

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>page</title>
</head>
<body>
<h1>{{ article.title }}</h1>
<br/>
<h3>{{ article.content }}</h3>
<br/><br/>
<a href="{% url 'blog:add_page' article.id %}">修改文章</a>
</body>
</html>


主页面index.html 添加新文章url中加 0

<h3><a href="{% url 'blog:add_page' 0 %}">添加新文章</a> </h3>


文章编辑页面add_article.html页面

如果有后台传入article对象,表单中加入value

{% if *** %}

{% else %}

{% endif %}

<!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 %}
{% if article %}
<label>文章标题
<input type="text" name="title" value="{{ article.title }}"/>
</label>
<p></p>
<label>文章内容
<input type="text" name="content" value="{{ article.content }}"/>
</label>
{% else %}
<label>文章标题
<input type="text" name="title"/>
</label>
<p></p>
<label>文章内容
<input type="text" name="content"/>
</label>
{% endif %}
<p></p>
<input type="submit" value="提交">
</form>
</body>
</html>


利用方法2来修改edit_action响应函数

文章编辑页面add_article.html页面

<!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 %}
{% if article %}
<input type="hidden" name="article_id" value="{{ article.id }}">
<label>文章标题
<input type="text" name="title" value="{{ article.title }}"/>
</label>
<p></p>
<label>文章内容
<input type="text" name="content" value="{{ article.content }}"/>
</label>
{% else %}
<input type="hidden" name="article_id" value="0">
<label>文章标题
<input type="text" name="title"/>
</label>
<p></p>
<label>文章内容
<input type="text" name="content"/>
</label>
{% endif %}
<p></p>
<input type="submit" value="提交">
</form>
</body>
</html>


修改views.py

后台获取article_id

如果为0,数据库创建新的对象

否则,取出数据库中对应对象,修改对象

修改对象:article.title = title    article.save()

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)
arts = models.Article.objects.all()
return render(request, 'blog/index.html', {'articles': arts})
arts = models.Article.objects.get(pk=article_id)
arts.title = title
arts.content = content
arts.save()
return  render(request, 'blog/page.html', {'article': arts})


打开服务器 http://localhost:8000/blog/


点击添加新文章,进入了http://localhost:8000/blog/edit/0,填写后可提交



点击第一篇修改的文章  进入了ttp://localhost:8000/blog/article/1



点击修改文章,进入了http://localhost:8000/blog/edit/1,修改后即可提交

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: