没有返回一个HttpResponse对象

时间:2012-09-20 02:08:18

标签: django requestcontext

我不知道为什么这不起作用,请帮忙

views.py

def addaNonProfit(request):
    """we need NonProfits.""" 
    if request.method == 'POST':
        form = NonProfitForm(request.POST)
        if form.is_valid():
            new_nonProfit = form.save(request.user)
            return HttpResponseRedirect(reverse(new_nonProfit, args=(new_nonProfit.slug,)))
    else:
        form = NonProfitForm()
        return render_to_response('addaNonProfit.html',{'form': form},context_instance=RequestContext(request))

def nonProfit(request,slug):
    """ we need a page to serve info about non profit pages"""
    np = get_object_or_404(NonProfit,slug=slug)
    return render_to_response('nonProfit.html',{'nonProfit':np},context_instance=RequestContext(request))

仍然无法正常工作

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/addaNonProfit/

Django Version: 1.4.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'registration',
 'DandT',
 'paypal.standard.ipn',
 'django.contrib.comments')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  129.                     raise ValueError("The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name))

Exception Type: ValueError at /addaNonProfit/
Exception Value: The view RaffleMe.views.addaNonProfit didn't return an HttpResponse object.

2 个答案:

答案 0 :(得分:3)

如果表单无效,则不会返回响应:

您可以将return render_to_response一个缩进块移到左侧

def addaNonProfit(request):
    """we need NonProfits.""" 
    if request.method == 'POST':
        form = NonProfitForm(request.POST)
        if form.is_valid():
            new_nonProfit = form.save(request.user)
            return HttpResponseRedirect(reverse(new_nonProfit, args=(new_nonProfit.slug,)))
    else:
        form = NonProfitForm()
    return render_to_response('addaNonProfit.html',{'form': form},context_instance=RequestContext(request))

答案 1 :(得分:2)

如果方法是POST且表单无效,则不返回任何内容。您必须从视图中的每个路径返回HttpResponse