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})
<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>
<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>
re_path('^index/$', views.index),
re_path('^article/(?P<article_id>[0-9]+)$', views.article_page, name='article_page'),
re_path('^edit/(?P<article_id>[0-9]+)$', views.edit_page, name='edit_page'),
re_path('^edit/action$', views.edit_action, name='edit_action'),