2013-11-24 163 views
0

我在django.I新都面臨的一個問題與表單驗證。視圖rest.views.submitreq沒有返回HttpResponse對象

錯誤是ValueError at/app/rest/submitreq_val/ 視圖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 %} 

現在請幫我解決這個錯誤。

+0

首先弄清楚什麼是'{{sub_req.as_p}}'。要顯示它您在forms.py創建的那些領域'{{sub_req.request}}'或'{{sub_req.category}}'這樣的,第二你沒有forms.py文件寫'請求= forms.CharField(max_length = 100,required = True)'。 –

回答

1

您的代碼段的縮進被打破,所以很難肯定地說,但它看起來像你只返回一個GET請求的響應。另一個分支不會顯式返回,這意味着它返回None而不是HttpResponse對象。

另外,該線路:

request = sub_req.cleaned_data['request'] 

陰影傳遞給你的看法HttpRequest對象。這在目前沒有任何區別,但是你可能在後面也需要HttpRequest對象。

1

嘗試這樣,並檢查它是否工作

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