提交表单(输入)后,它将重定向到另一个页面(结果)。 问题是它确实会转到所需的URL,但内容是空的。 (如果我单独打开它不是空的)。提前感谢您的建议。
输入 - 网址
from inputform import views
from django.views.generic.list import ListView
from inputform.views import input
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^result_list/$',ResultView.as_view(),name='result'),
(r'^input/$', RedirectView.as_view(url='/result_list/')),
}
输入 - 视图
from django.http import HttpResponseRedirect
from django.shortcuts import render,render_to_response,get_object_or_404
from inputform.forms import Inputform
from inputform.models import Input
from dupont.models import Result
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.list import ListView
@csrf_exempt
def input(request):
if request.method == 'POST':
form = Inputform(request.POST)
if form.is_valid():
cd = form.cleaned_data
print (cd['company'])
form.save()
return redirect(reverse,('','result_list'))
结果--views.py
class ResultView(ListView):
context_object_name = 'result_list'
template_name = 'result_list.html'
queryset = Result.objects.all()
def get_context_data(self, **kwargs):
context = super(ResultView, self).get_context_data(**kwargs)
context['input'] = Input.objects.all()
return context

结果 - html
<form action="result_list/" method="post">{% csrf_token %}
<div class="field" >
<label> Select the Region:
{{ form.regionlist }}
{% for region in form.regionlist.choices %}
<option value="{{ val }}" {% ifequal data.val val %}selected {% endifequal %}></option>
{% endfor %}
</label>
</div>
<div class="fieldWrapper">
<p><input type="submit" value="Submit" /></p></div>
</form>
答案 0 :(得分:1)
如评论中所述,传递给reverse()
的正确参数是“结果”,即网址的名称。
如果您使用django 1.7或更高版本,您可以跳过反向部分,django会自动为您完成。
def input(request):
if request.method == 'POST':
form = Inputform(request.POST)
if form.is_valid():
cd = form.cleaned_data
print (cd['company'])
form.save()
return redirect('result')
应该适合你