美文网首页
【django】【my_blog】编辑功能

【django】【my_blog】编辑功能

作者: JerichoPH | 来源:发表于2018-05-07 20:12 被阅读54次

编辑功能

编辑页面:

  • 打开文件:blog/templates/article/details.html,并修改:
<h2>{{ article_details.title }}</h2>
<h3>{{ article_details.content }}</h3>
<p>
    <a href="{% url 'blog:article$index_page' %}">返回列表</a>
    &nbsp;&nbsp;
    <a href="{% url 'blog:article$edit_page' article_details.id %}">修改</a>
</p>
  • 修改:blog/view/article.py,添加如下方法:
# 编辑页面
def edit_page(request, article_id='0'):
    # 如果article_id == '0',则视为创建新文章
    if article_id == '0':
        return render(request=request, template_name='article/edit.html')
    else:
        article_details = models.article.Article.objects.get(pk=article_id)
        return render(request=request, template_name='article/edit.html', context={'article_details': article_details})
  • 创建模板文件:blog/templates/article/edit.html,并写入:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文章详情页:{{ article_details.title }}</title>
</head>
<body>
{% if article_details.id %}
   <h2>修改文章</h2>
{% else %}
   <h3>创建文章</h3>
{% endif %}
<form action="{% url 'blog:article$edit_action' %}" method="post">
    {% csrf_token %}
    <input type="hidden" name="article_id" value="{{ article_details.id | default:'0' }}">
    <input type="text" name="article_title" value="{{ article_details.title }}">
    <br>
    <input type="text" name="article_content" value="{{ article_details.content }}">
    <br>
    <br>
    <button>保存</button>
</form>
</body>
</html>
  • 加入URL,修改blog/url.py
url(regex=r'^article/edit/(?P<article_id>[0-9]+)/$', view=article.edit_page, name='article$edit_page'),
url(regex=r'^article/edit_action/$', view=article.edit_action, name='article$edit_action')

查看结果,访问:http://localhost:8000/blog/artilce/detials/1/

image
image

编写功能:

  • 修改blog/view/article.py,加入edit_action方法:
# 保存编辑
def edit_action(request):
    article_id = request.POST.get('article_id', 0)
    article_title = request.POST.get('article_title')
    article_content = request.POST.get('article_content')
    if article_id == 0:
        # 创建文章
        article_model = models.article.Article(title=article_title, content=article_content)
        try:
            # 写入成功,返回列表页
            article_model.save()
            return index_page(request=request)
        except Exception, e:
            # 打印错误
            print(e)
    else:
        # 修改文章
        article_details = models.article.Article.objects.get(pk=article_id)
        article_details.title = article_title
        article_details.content = article_content
        try:
            # 修改成功,返回详情页面
            article_details.save()
            return details_page(request=request, article_id=article_id)
        except Exception, e:
            # 打印错误
            print(e)

查看结果,访问:http://localhost:8000/blog/article/edit/1/

image
image
image

编写新建功能

  • 修改blog/templates/article/index.html,并修改:
{#由于我们通过article_id是否为0,视为修改或创建的标签,则在该URL中路由参数写0#}
<p><a href="{% url 'blog:article$edit_page' 0 %}">新建文章</a></p>

查看结果,访问http://localhost:8000/blog/article/index/

相关文章

网友评论

      本文标题:【django】【my_blog】编辑功能

      本文链接:https://www.haomeiwen.com/subject/ynzvrftx.html