编辑您的视图,
from django.shortcuts import redirect, render
def home(request):
return render(request, 'home.html')
def index(request):
if request.method == 'POST':
form = PageForm(request.POST)
if form.is_valid():
form.save()
#No need to call index view again,
#it's already been called.
return redirect('home') #redirection.
else:
form = PageForm()
return render(request, 'index.html', {'form':form})
urls.py
from . import views
url(r'^index/$', views.index, name='index'),
url(r'^home/$', views.home, name='home')
您可以使用render() 快捷功能,
https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#render
render(request, template[, dictionary][, context_instance][, content_type][, status][, current_app])
render() 是 1.3 中用于 render_to_response 的全新快捷方式,它将自动使用我从现在开始肯定会使用的 RequestContext。
您的 html 表单中的 action 部分是您定义表单提交的端点或 URL 的地方。由于您的视图接受发布数据,因此您的action='' 需要保持为空,这意味着您将发布到同一个视图本身。