0

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

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.
4

2 回答 2

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))
于 2012-09-20T04:52:19.450 回答
2

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

于 2012-09-20T04:48:57.930 回答