djangorequestcontext

didn't return an HttpResponse object


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.

Solution

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