视图rest.views.submitreq未返回HttpResponse对象

时间:2013-11-24 06:52:22

标签: python django

我是django的新手。我遇到了表单验证问题。

错误是 / app / rest / submitreq_val /的ValueError 视图rest.views.submitreq未返回HttpResponse对象。

这是我的forms.py

 from django import forms
 from django.core.exceptions import ValidationError

 from request.models import Request

 class SubreqForm(forms.Form):
    request = forms.CharField(max_length=100)
    category = forms.CharField()
    sub_category = forms.CharField()

这是我的表单验证的views.py

 from django.http import HttpResponse
 from django.views.decorators.csrf import csrf_exempt
 from request.models import Request
 from django.views.generic import TemplateView, View
 from django.http import HttpResponse
 from django.template import Context,RequestContext, loader
 from django.shortcuts import render_to_response
 from django.core.context_processors import csrf
 from django.shortcuts import render
 from django.http import HttpResponseRedirect
 from rest.forms import SubreqForm

 def submitreq(request):

if request.method == "POST":
    sub_req = SubreqForm(request.POST)

    if sub_req.is_valid():
        success = True
        request = sub_req.cleaned_data['request']
        category = sub_req.cleaned_data['category']
        sub_category = sub_req.cleanded_data['sub_category']
else:
    sub_req = SubreqForm()
    ctx = {'sub_req': sub_req}
    return render_to_response("rest/test.html",ctx,context_instance=RequestContext(request))

这是我的模板......

{% block title %}
  Test
{% endblock %}

{% block content %}
  <form action = "" method = "post">
  {{sub_req.as_p}}
  <input type = "submit" value = "send">
{% endblock %}

现在请帮我解决这个错误。

2 个答案:

答案 0 :(得分:1)

您的代码段缩进已损坏,因此很难确定,但看起来您只是返回GET请求的响应。另一个分支没有显式返回,这意味着它返回None而不是HttpResponse对象。

此外,这一行:

request = sub_req.cleaned_data['request']

隐藏传递给视图的HttpRequest对象。目前没有任何区别,但您可能稍后会在此分支中使用HttpRequest对象。

答案 1 :(得分:1)

尝试这样并检查它是否正常工作

sub_req = SubreqForm()
ctx = {'sub_req': sub_req}
context = RequestContext(request, ctx)
return render_to_response("rest/test.html", context_instance=context)