我想創建一個表單插入多個頁面。在下面的例子中,我創建了一個「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" %}
....