Home > Blockchain >  Getting a didn't return an HTTPResponse Object
Getting a didn't return an HTTPResponse Object

Time:01-17

I am creating a view in Django that will take user input based on inventory and link it to the different locations in which inventory is taken. When I try to get it up and running, I get the view inventory.views.inventory_entry didn't return an HttpResponse object. It returned None instead.

Code below Views.py

from django.shortcuts import render, redirect
from .models import Location, Inventory
from .forms import DataForm 
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404

def inventory_entry(request):
    if request.method == 'POST':
        form = DataForm(request.POST)
        if form.is_valid():
            location = form.cleaned_data['location']
            date = form.cleaned_data['date']
            description = form.cleaned_data['description']
            in_use = form.cleaned_data['in_use']
            training_room = form.cleaned_data['training_room']
            conference_room = form.cleaned_data['conference_room']
            gsm_office = form.cleaned_data['gsm_office']
            prospecting_station = form.cleaned_data['prospecting_station']
            applicant_station = form.cleaned_data['applicant_station']
            visitor_station = form.cleaned_data['visitor_station']
            other = form.cleaned_data['other']
            spare_on_floor = form.cleaned_data['spare_on_floor']
            spare_storage = form.cleaned_data['spare_storage']
            total_spare = form.cleaned_data['total_spare']
            broken = form.cleaned_data['broken']
            total = form.cleaned_data['total']
            
            Inventory.objects.create(location=location, date=date, description=description, 
                                     in_use=in_use, training_room=training_room, conference_room=conference_room,
                                     gsm_office=gsm_office, prospecting_station=prospecting_station, applicant_station=applicant_station,
                                     visitor_station=visitor_station, other=other, spare_on_floor=spare_on_floor, spare_storage=spare_storage,
                                     total_spare=total_spare, broken=broken, total=total)
            return redirect('data_entry')
        
        else:
            form = DataForm()
            
        locations = Location.objects.all()
        return render(request, "inventory/index.html", {'form':form, 'locations': locations})

Models.py

from django.db import models

OFFICE_CHOICES = (
    ('Akron, OH', 'AKRON'),
    ('Atlanta, GA', 'ATLANTA'),
    ('Austin, TX', 'AUSTIN'),
    ('Birmingham, AL', 'BIRGMINGHAM'),
    ('Boston, MA', 'BOSTON'),
    ('Charleston, SC', 'CHARLESTON_SC'),
    ('Charleston, WV', 'CHARLESTON_WV'),
    ('Charlotte, NC', 'CHARLOTTE'),
    ('Chicago West, IL', 'CHICAGO_WEST'),
    ('Chicago, IL', 'CHICAGO'),
    ('Cleveland, OH', 'CLEVELAND'),
    ('Denver, CO', 'DENVER'),
    ('Dallas, TX', 'DALLAS'),
    ('Evansville, IN', 'EVANSVILLE'),
    ('Fayetteville, AR', 'FAYETTEVILLE'),
    ('Ft Lauderdale, FL', 'FT LAUDERDALE'),
    ('Ft Wayne, IN', 'FT WAYNE'),
    ('Grand Rapids, MI', 'GRAND RAPIDS'),
    ('Greensboro, NC', 'GREENSBORO'),
    ('Greenville, SC', 'GREENVILLE'),
    ('Houston, TX', 'HOUSTON'),
    ('Indianapolis, IN', 'INDIANAPOLIS'),
    ('Jacksonville, FL', 'JACKSONVILLE'),
    ('Kansas City, MO', 'KANSAS CITY'),
    ('Knoxville, TN', 'KNOXVILLE'),
    ('Las Vegas, NV', 'LAS VEGAS'),
    ('Lexington, KY', 'LEXINGTON'),
    ('Louisville, KY', 'LOUISVILLE'),
    ('Memphis, TN', 'MEMPHIS'),
    ('Milwaukee, WI', 'MILWAUKEE'),
    ('Minneapolis, MN', 'MINNEAPOLIS'),
    ('Mobile, AL', 'MOBILE'),
    ('Nashville, TN', 'NASHVILLE'),
    ('New Orleans, LA', 'NEW ORLEANS'),
    ('Orlando, FL', 'ORLANDO'),
    ('Phoenix, AZ', 'PHOENIX'),
    ('Pittsburgh, PA', 'PITTSBURGH'),
    ('Portland, OR', 'PORTLAND'),
    ('Raleigh, NC', 'RALEIGH'),
    ('Richmond, VA', 'RICHMOND'),
    ('Salt Lake City, UT', 'SALT LAKE CITY'),
    ('San Antonio, TX', 'SAN ANTONIO'),
    ('Savannah, GA', 'SAVANNAH'),
    ('St Louis, MO', 'ST LOUIS'),
    ('Tampa, FL', 'TAMPA'),
    ('Toledo, OH', 'TOLEDO'),    
)

class Location(models.Model):
    location_name = models.CharField(max_length=20, choices=OFFICE_CHOICES, default='Select the office')
def __str__(self):
    return self.location_name

class Inventory(models.Model):
    location = models.ForeignKey(Location, on_delete=models.CASCADE)
    # location = models.CharField(max_length=20, choices=OFFICE_CHOICES, default='Select the office')
    date = models.DateField()
    description = models.CharField(max_length=30)
    in_use = models.PositiveIntegerField()
    training_room = models.PositiveIntegerField()
    conference_room = models.PositiveIntegerField()
    gsm_office = models.PositiveIntegerField()
    prospecting_station = models.PositiveIntegerField()
    applicant_station = models.PositiveIntegerField()
    visitor_station = models.PositiveIntegerField()
    other = models.PositiveIntegerField()
    spare_on_floor = models.PositiveIntegerField()
    spare_storage = models.PositiveIntegerField()
    total_spare = models.PositiveIntegerField()
    broken = models.PositiveIntegerField()
    total = models.PositiveIntegerField()
    
def __str__(self):
    return f"Data for location {self.location} on {self.date}"

forms.py

from django import forms
from .models import Location

class DataForm(forms.Form):
    location = forms.ModelChoiceField(queryset=Location.objects.all())
    date = forms.DateField()
    description = forms.CharField(widget=forms.Textarea)
    in_use = forms.IntegerField()
    training_room = forms.IntegerField()
    conference_room = forms.IntegerField()
    gsm_office = forms.IntegerField()
    prospecting_station = forms.IntegerField()
    applicant_station = forms.IntegerField()
    visitor_station = forms.IntegerField()
    other = forms.IntegerField()
    spare_on_floor = forms.IntegerField()
    spare_storage = forms.IntegerField()
    total_spare = forms.IntegerField()
    broken = forms.IntegerField()
    total = forms.IntegerField()

I have tried the above code. I am hoping I can get this to render in an HTML page to take users' input and attach it to the different locations.

CodePudding user response:

You are not handling requests which are not POST (probably indentation problem). You can change your code like this:

def inventory_entry(request):
    if request.method == 'POST':
        form = DataForm(request.POST)
        ...
        if form.is_valid():
           return redirect('data_entry')
        # no need to handle form invalid , otherwise it will not render form errors. Also, the final return will return this form either way.

    else:
        form = DataForm()
    locations = Location.objects.all()
    return render(request, "inventory/index.html", {'form':form, 'locations': locations})
     
    

CodePudding user response:

Your data_entry view does not return Http Object. You need to make sure, that every possible situation has some HttpResponse or redirect in that view.

  • Related