我想创建一个表单以插入多个页面。在下面的示例中,我创建了一个模板“form.html”并尝试输入“index.html”。我已经有一个索引视图(在本例中进行了简化)。因此,我不知道如何调用 myform 视图将表单包含在索引中。
# urls.py
urlpatterns = patterns('',
url(r'^$', 'index'),
url(r'^form$', 'myform'),
)
# views.py
def index(request):
return render(request, 'index.html')
def myform(request):
if request.method == "POST":
form = MyForm(request.POST)
if form.is_valid():
request.session['name'] = form.cleaned_data['name']
return HttpResponseRedirect('/form')
else:
form = MyForm()
args = {'form': form}
return render(request, 'form.html', {'form': form})
# form.html
<form action="/form/ok" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
# index.html
....
{% include "form.html" %}
....