1. 程式人生 > >Django學習:修改文章和新增文章(11)

Django學習:修改文章和新增文章(11)

部落格頁面的修改文章和新增新文章

從主頁點選不同文章的超連結進入文章頁面,就是傳遞了一個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,修改後即可提交