Anjitha
Anjitha

Reputation: 59

Nothing shown in in the view.html page

The view login.views.IndexView didn't return an HttpResponse object. It returned None instead.

class IndexView(FormView):
    form_class = NameForm
    template_name = 'reg.html'
    success_url='display'
    def form_valid(self, form):
        form.save()
        return super().form_valid(form)

class DisplayView(generic.TemplateView):
    template_name = 'view.html'
    def dis(self,request):
        st4=[]
        st5=[]
        ob=Register.objects.all()
        for i in ob:
            st4.append(i.name)
            st5.append(i.rollno)
        return render(request,'view.html',{'st4':st4,'st5':st5})

Upvotes: 2

Views: 58

Answers (2)

Anjitha
Anjitha

Reputation: 59

class IndexView(FormView):
    form_class = NameForm
    template_name = 'reg.html'
    success_url='display'
    def form_valid(self, form):
        form.save()
        return super().form_valid(form)

def dis(request):
    st4=[]
    st5=[]
    ob=Register.objects.all()
    for i in ob:
        st4.append(i.name)
        st5.append(i.rollno)
    return render(request,'view.html',{'st4':st4,'st5':st5})

Upvotes: 0

Alasdair
Alasdair

Reputation: 309089

Your form_valid method should return a response. For a FormView, the easiest thing to do would be to call super() and return that.

def form_valid(self, form):
    form.save()
    return super(IndexView, self).form_valid(form)

In your DisplayView, you've written a dis method but you haven't written any code that will actually call it. The simplest thing to do would to use a function-based-view instead.

def dis(request):
    st4=[]
    st5=[]
    ob=Register.objects.all()
    for i in ob:
        st4.append(i.name)
        st5.append(i.rollno)
    return render(request,'view.html',{'st4':st4,'st5':st5})

Then change your url pattern to use views.dis instead of views.DisplayView.as_view().

If you really want to use TemplateView, then you could override get_context_data and add st4 and st5 to the context there.

class DisplayView(generic.TemplateView):
    template_name = 'view.html'

    def get_context_data(self, **kwargs):
        context = super(DisplayView, self).get_context_data(**kwargs)

        st4=[]
        st5=[]
        ob=Register.objects.all()
        for i in ob:
            st4.append(i.name)
            st5.append(i.rollno)
        context['st4'] = st4
        context['st5'] = st5
        return context

Unless you're familiar with class-based-views, the function-based view is easier to understand so I would stick with that. The TemplateView is not automatically better than the function based view.

Upvotes: 1

Related Questions