Django学习(八)---修改文章和添加文章

时间:2023-03-09 01:06:49
Django学习(八)---修改文章和添加文章

博客页面的修改文章和添加新文章

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

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

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

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

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

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

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

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

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

urls.py中对应url加上article_id和name:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^index/$',views.index),
    url(r'^article/(?P<article_id>[0-9]+)/$',views.page,name='page'),
    url(r'^addarticle/(?P<article_id>[0-9]+)/$',views.add_article,name='add_article'),
    url(r'^subarticle/$',views.sub_article,name='sub_article'),

]

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

<!DOCTYPE html>
<html>
<head>
    <title>my page</title>
</head>
<body>
<h1>{{ article.title }}</h1>
<br/>
<h3>{{ article.content }}</h3>
<br/><br/>
<a href="{% url 'blog:edit_article' article.id %}">修改文章</a>
</body>
</html>

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

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

文章编辑页面add_article.html页面:

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

{% if *** %}

{% else %}

{% endif %}

<!DOCTYPE html>
<html>
<head>
    <title>增加新文章</title>
</head>
<body>
<form action="{% url 'blog:sub_article' %}" method="post">
    {% csrf_token %}
    {% if article %}
        文章标题<input type="text" name='title' value='{{ article.title }}'/>
        <br>
        文章内容<input type="text" name='content' value='{{ article.content }}'/>
        <br>
    {% else %}
        文章标题<input type="text" name='title' />
        <br>
        文章内容<input type="text" name='content'/>
        <br>
        <input type="submit" value="提交">
    {% endif %}
</form>
</body>
</html>

2) 利用方法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 sub_article(request):
    title = request.POST.get('title','TITLE')
    content = request.POST.get('content','CONTENT')
    article_id = request.POST.get(')
    ':
        models.Article.objects.create(title=title, content=content)
        articles = models.Article.objects.all()
        return render(request, 'blog/index.html', {'articles': articles})
    articles = models.Article.objects.get(pk=article_id)
    articles.title = title
    articles.content = content
    articles.save()
    return  render(request, 'blog/page.html', {'article': articles})