目前,我试图使其能够在页面上完成一个问题,然后当它被提交到数据库时,下一页将加载。此外,每当我指向模板文件夹时,它都会捎带原始文件夹,这意味着它无法找到新的HTML页面。
我的想法是,当问题1完成时,它将链接到问题2,其中def question2代码将执行,依此类推。但表格不能正确显示,我相信这是由于def问题2没有正确运行。
def question1(request):
question_form1 = QuestionForm1()
if request.method == 'POST':
form = QuestionForm1(request.POST)
if form.is_valid():
form.save() # saves to database
return HttpResponse('question2.html')
else:
return render(request, 'music/failed.html')
return render(request, 'music/question1.html', locals())
def question2(request):
question_form2 = QuestionForm2()
if request.method == 'POST':
form2 = QuestionForm2(request.POST)
if form2.is_valid():
form2.save() # Saves to database
return render(request, 'music/question3.html', locals())
else:
return render(request, 'music/failed.html')
return render(request, 'music/question2.html', locals())
修改:添加了Urls.py
from django.conf.urls import url
from . import views
app_name = 'music'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^register/$', views.register, name='register'),
url(r'^login_user/$', views.login_user, name='login_user'),
url(r'^logout_user/$', views.logout_user, name='logout_user'),
url(r'^question1/$', views.question1, name='question1'),
url(r'^question2/$', views.question2, name='question2'),
]
答案 0 :(得分:0)
from django.http import HttpResponseRedirect
from django.urls import reverse
# delete following line
return HttpResponse('question2.html')`
# replace with this one
return HttpResponseRedirect(reverse('view_name_here'))
# or if you are using any namespaces for your url
return HttpResponseRedirect(reverse('namespace:view_name_here'))