Django部落格修改文章
阿新 • • 發佈:2018-12-02
一 思路
新文章為空,修改文章有內容
修改文章頁面有文章物件
通過文章的ID區分:0為新文章,其他為修改文章
二 修改資料方法
article.title = title
article.save()
三 程式碼
1 blog/views.py
from django.shortcuts import render from django.http import HttpResponse from . import models def index(request): articles = models.Article.objects.all() return render(request, 'blog/index.html',{'articles': articles}) def article_page(request,article_id): article = models.Article.objects.get(pk=article_id) return render(request,'blog/article_page.html',{'article':article}) def edit_page(request,article_id): if str(article_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}) 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) 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})
2 blog/urls.py
from django.conf.urls import url, include 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'), url(r'^edit/action$', views.edit_action,name='edit_action'), ]
3 blog/templates/blog/article_page.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>article page</title> </head> <body> <h1>{{article.title}}</h1> <br/> <h3>{{article.content}}</h3> <br/><br/> <a href="{% url 'blog:edit_page' article.id %}">修改文章</a> </body> </html>
4 blog/templates/blog/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>
<a href="{% url 'blog:edit_page' 0 %}">新文章</a>
</h1>
{% for article in articles %}
<a href="{% url 'blog:article_page' article.id %}">{{ article.title }}</a>
<br/>
{% endfor %}
</body>
</html>
5 blog/templates/blog/edit_page.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Edit Page</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>
<br/>
<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>
<br/>
<label>文章內容
<input type="text" name="content"/>
</label>
<br/>
{% endif %}
<input type="submit" value="提交">
</form>
</body>
</html>
四 測試
1 瀏覽器輸入http://localhost:8000/blog/article/1
2 點選修改文章
3 修改後儲存