Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django says "didn't return an HttpResponse object. It returned None instead."

Tags:

python

django

I was looking for solutions on stack, but none did helped me. The most solutions were indent-related, but I think mine is not. I'll appreciate it when someone can help me out with this!

Here is what i have:

models.py

from django.db import models

class QuestionPost(models.Model):
    question = models.CharField(max_length=1000)
    tag = models.CharField(max_length=200)
    pub_date = models.DateTimeField('Date published')


class AnswerPost(models.Model):
    answer_text = models.CharField(max_length=1000)
    answer_rate = models.IntegerField()

forms.py

from django import forms

# Create your models here.

class QuestionPostForm(forms.Form):
    question = forms.CharField(label='Question text', max_length=1000)
    tag = forms.CharField(label='Tags', max_length=200)
    pub_date = forms.DateTimeField(label='Date published')


class AnswerPostForm(forms.Form):
    answer_text = forms.CharField(label='Answer Text', max_length=1000)
    answer_rate = forms.IntegerField(label='Rate')

views.py

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from .forms import QuestionPostForm
from .models import QuestionPost
from django.template import RequestContext

# Create your views here.

def get_question(request):
    if request.method == 'POST':
        form = QuestionPostForm(request.POST)
        if form.is_valid():
            obj = QuestionPost()
            obj.question = form.cleaned_data['question']
            obj.tag = form.cleaned_data['tag']
            obj.save()
            return HttpResponseRedirect('forum/index.html',{'form':form})

        else:
            form = QuestionPostForm()
        return render_to_response(request, 'forum/index.html', {'form': form})

Why do i get the error: didn't return an HttpResponse object. It returned None instead? I don't understand? I'm a beginner in Django and this will help me learn something..

like image 978
Floris de Boer Avatar asked Feb 04 '23 09:02

Floris de Boer


1 Answers

Please check now, In get request you were not returning anything,

def get_question(request):
    if request.method == 'POST':
        form = QuestionPostForm(request.POST)
        if form.is_valid():
            obj = QuestionPost()
            obj.question = form.cleaned_data['question']
            obj.tag = form.cleaned_data['tag']
            obj.save()
            return HttpResponseRedirect('forum/index.html',{'form':form})

    else:
        form = QuestionPostForm()
    return render_to_response(request, 'forum/index.html', {'form': form})
like image 143
Sagar Avatar answered Feb 16 '23 02:02

Sagar