w_lpz w_lpz - 6 months ago 24
Python Question

The view producer.pfviews.podcastsetup didn't return an HttpResponse object. It returned None instead

I am creating a podcast management website where you have to register a user w/ django redux registration and provide a username, email & password. When thats done, it takes them straight to a dashboard where they can setup their podcast. When they are going to set it up, I want them to provide me with a

company_name
,
first_name
and
last_name
and save it. This is how my views is set up for it:

from django.conf import settings
from django.shortcuts import render

from .forms import PodcastSetupForm

from .models import Client

def podcastsetup(request):

title = 'Podcast Setup'
title_align_center = True
form = PodcastSetupForm(request.POST or None)
context = {
"title": title,
"form": form
}

if form.is_valid():

instance = form.save(commit=False)

company_name = form.cleaned_data.get("company_name")
first_name = form.cleaned_data.get("first_name")
last_name = form.cleaned_data.get("last_name")

instance.company_name = company_name
instance.first_name = first_name
instance.last_name = last_name

instance.save()

return render(request, "forms.html", context)


When I try to get in the website (http://xxx/podcastsetup/), I get the following error:

Environment:


Request Method: GET
Request URL: http://xxx/podcastsetup/

Django Version: 1.9
Python Version: 2.7.6
Installed Applications:
('producer',
'django.contrib.admin',
'django.contrib.sites',
'registration',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'randomslugfield',
'adminsortable2',
'crispy_forms')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')



Traceback:

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
158. % (callback.__module__, view_name))

Exception Type: ValueError at /podcastsetup/
Exception Value: The view producer.pfviews.podcastsetup didn't return an HttpResponse object. It returned None instead.


This is what I have in my forms.py:

from django import forms

from .models import Client

class PodcastSetupForm(forms.ModelForm):

class Meta:

model = Client
fields = ['company_name', 'first_name', 'last_name']

def clean_company_name(self):
company_name = self.cleaned_data.get('company_name')

return company_name

def clean_first_name(self):
first_name = self.cleaned_data.get('first_name')

return first_name

def clean_last_name(self):
last_name = self.cleaned_data.get('last_name')

return last_name


My urls.py:

from django.conf.urls import include, url
from django.conf import settings
from django.contrib import admin
from django.conf.urls.static import static

admin.site.site_header = 'FullCast Admin'

urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('producer.urls')),
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^dashboard/', 'producer.pfviews.dashboard', name="dashboard"),
url(r'^podcastsetup/', 'producer.pfviews.podcastsetup', name="podcastsetup"),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


And the
Client
class in models.py:

from django.db import models
from django.conf import settings
from django.utils.encoding import smart_text
from producer import myFields
from randomslugfield import RandomSlugField
from django.contrib.auth.models import User

class Client(TimeStampedModel):
company_name = models.CharField(max_length=64)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
email = models.EmailField()
user = models.ForeignKey(User, on_delete=models.CASCADE)

def full_name(self):
return smart_text('%s %s' % (self.first_name, self.last_name))

def __unicode__(self):
return smart_text(self.company_name)


Would appreciate some help

Answer
def podcastsetup(request):

    title = 'Podcast Setup'
    title_align_center = True
    form = PodcastSetupForm(request.POST or None)
    context = {
        "title": title,
        "form": form
    }

    if form.is_valid():

        instance = form.save(commit=False)

        company_name = form.cleaned_data.get("company_name")
        first_name = form.cleaned_data.get("first_name")
        last_name = form.cleaned_data.get("last_name")

        instance.company_name = company_name
        instance.first_name = first_name
        instance.last_name = last_name

        instance.save()

        return render(request, "forms.html", context)
    else:
        return render(request, "forms.html", context)

Try this, you need to return a response even if the form is not valid.

Comments