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

Hello everyone,

In the html form I created, when I leave the textfield blank (culture, status and hospital_sample_number), ValueError at /addsample_row
The view App.views.addsample_row didn’t return an HttpResponse object. It returns None instead error is displayed. Even if I change null=True to blank=True, still same error occurs.

What I am doing wrong? Thank you in advance.

class Samples(models.Model):

    reception_date = models.DateField()
    hospital_date = models.DateField()
    culture = models.CharField(max_length=128, null=True)
    index_sample = models.IntegerField(blank=True, null=True)
    is_index_sample = models.BooleanField(null=True)
    status = models.CharField(max_length=128, null=True)
    hospital_sample_number = models.CharField(max_length=256, null=True)
def addsample_row(request):
    if request.method == "POST":
        if request.POST.get('culture') \
            and request.POST.get('index_sample') \
            and request.POST.get('is_index_sample') \
            and request.POST.get('status') \
            and request.POST.get('hospital_sample_number'):
 
           sample = Samples()

           sample.culture = request.POST.get('culture')
           sample.index_sample = request.POST.get('index_sample')
           sample.is_index_sample = request.POST.get('is_index_sample')
           sample.status = request.POST.get('status')
           sample.hospital_sample_number = request.POST.get('hospital_sample_number')

           sample.save()

           messages.success(request, "New sample is added successfully!")

           return HttpResponseRedirect("/")
   else:
       return render(request, 'addsample.html')

You’re not handling one of the possible conditions in your code.

If the request.method is “POST”, but one of your request.POST.get calls fails, you have no code to handle it.

Side note: You really should create a Django form for this and let Django do a lot of this work.

Yeah probably you are right that I need to use django form. Problem was about hospital_sample_number field is empty at my own form because I do not fill the field on purpose. Then,

I just changed the line:
and request.POST.get('hospital_sample_number'):
to
and (request.POST.get('hospital_sample_number') or request.POST.get('hospital_sample_number') is '')

And now, it worked! You think that way is convenient?

I think you’ve written a lot more code than you needed to write - and still not enough code there for what I would call a “production-quality” view.

You’ve got no error handling being done, and no validation of the data being submitted.

As just one example, you have defined index_sample as an IntegerField, but you’re not validating that the data submitted in that field is actually an integer.

You’re not performing any error checking or validation on any of your fields. That’s just one of the features that Django forms provides.

I suggest you review the docs and examples on the Working with forms page.

2 Likes