我在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 %}
現在請幫我解決這個錯誤。
首先弄清楚什麼是'{{sub_req.as_p}}'。要顯示它您在forms.py創建的那些領域'{{sub_req.request}}'或'{{sub_req.category}}'這樣的,第二你沒有forms.py文件寫'請求= forms.CharField(max_length = 100,required = True)'。 –