Héléna
Héléna

Reputation: 1095

Django: after submitting the form, why the redirected url is an empty page

After submitting the form (input), it will redirect to another page(result). The thing is it will indeed go to the desired url, but the content is empty. (It is not empty if I open it separately). Thanks for your suggestion in advance.

input -- 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/')),
      }

input --views

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'))

Result --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

Result -- 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>

Upvotes: 0

Views: 2041

Answers (1)

Resley Rodrigues
Resley Rodrigues

Reputation: 2288

As mentioned in the comments the correct parameter to pass to reverse() is 'result', i.e the name of the url.

If you're using django 1.7 or higher you can skip the reverse part and django will do it automatically for you.

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')

should work for you

Upvotes: 1

Related Questions