didn't return an HttpResponse object

▼魔方 西西 提交于 2019-12-11 19:15:39

问题


I don't know why this is not working, please help

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))

still not working

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.

回答1:


You are not returning a response if form is not valid:

You can move return render_to_response one indentation block to the left

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))



回答2:


If the method is POST and the form is invalid, you don't return anything. You have to return an HttpResponse from every path in your view.



来源:https://stackoverflow.com/questions/12505250/didnt-return-an-httpresponse-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!