码迷,mamicode.com
首页 > 编程语言 > 详细

python 3.5 django 笔记(六)修改博客标题与内容

时间:2017-06-12 14:54:45      阅读:627      评论:0      收藏:0      [点我收藏+]

标签:python3 django

技术分享



技术分享




接下来,要把博客继续完善

画个画儿先~~


~~~~~~

~~~~~~

主页点击修改文章----修改文章页面

主页点击新文章----新文章页面

新文章提交后----主页

修改文章提交后----修改文章

~~~~~~

~~~~~~


技术分享




技术分享

敲代码咯

编辑edit_page.html页面


<body>


<form action="{% url ‘blog:edit_action‘ %}" method="post">
{% csrf_token %}
<!--添加if语句,判断断码如果文章ID号不为0则执行-->
    {% if article %}
    <input type="hidden" name="article_id" value="{{ article.id }}"/>
    <label>文章标题
        <input type="text" name="title" value="{{ article.title }}"/>
            <!--在编辑栏羡慕文章内容-->
    </label>
    <br/>
    <label>文章内容
        <input type="text" name="content" value="{{ article.content }}"/>
            <!--在编辑栏羡慕文章内容-->
    </label>
    <br/>
    {% else %}
        <input type="hidden" name="article_id" value="0"/>
        <label>文章标题
        <input type="text" name="title" />
        </label>
    <br/>
        <label>文章内容
        <input type="text" name="content" />
        </label>
    <br/>
    {% endif %}
    <input type="submit" value="提交">
</form>


</body>



页面修改后,就去views.py操作执行动作

这次需要修改两次,信息量比较大咯

def edit_page(request, article_id):
    #添加个id参数
    if str(article_id) == ‘0‘:
    #判断字符型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})
    #不为0则显示内容

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语句
    if article_id == ‘0‘:
    #ID为0 ,则返回主页
        models.Article.objects.create(title=title, content=content)
        articles = models.Article.objects.all()
        return render(request,‘blog/index.html‘,{‘articles‘:articles})
    article = models.Article.objects.get(pk=article_id)
    article.title = title
    article.content = content
    article.save()
    return render(request, ‘blog/article_page.html‘, {‘article‘: article})
    #不是的话,就执行更新操作


最后,修改blog下的urls.py


from django.conf.urls import url

from . import views

urlpatterns = [
    url(r‘^index/$‘, views.index),
    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‘),
    #把编辑的页面也添加上id号识别
    url(r‘^edit/action$‘, views.edit_action, name=‘edit_action‘),

]



技术分享技术分享技术分享技术分享


技术分享

技术分享


搞定囖~~


python 3.5 django 笔记(六)修改博客标题与内容

标签:python3 django

原文地址:http://rexchow.blog.51cto.com/11619161/1934412

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!