本文介绍了Django说“没有返回HttpResponse对象”。相反,它返回None。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在寻找解决方案,但是没有一个帮助我。多数解决方案与缩进有关,但我认为并非如此。当有人可以帮助我解决这个问题时,我将不胜感激!
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!
这是我所拥有的:
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')
$来自django的b
$ b
views.py
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})
我为什么收到错误:未返回HttpResponse对象。它返回None?我听不懂我是Django的初学者,这将帮助我学习一些东西。
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..
推荐答案
请立即检查,在获取请求时,不返回任何内容,
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})
这篇关于Django说“没有返回HttpResponse对象”。相反,它返回None。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!