django 获取未返回HTTPResponse对象

3phpmpom  于 2023-01-18  发布在  Go
关注(0)|答案(2)|浏览(142)

我在Django中创建了一个视图,它将根据库存接受用户输入,并将其链接到不同的库存位置。当我尝试启动并运行它时,我得到了inventory视图。views.inventory_entry没有返回HttpResponse对象,而是返回了None。
代码见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()

我已经尝试了上面的代码。我希望我可以得到这个呈现在一个HTML页面,以接受用户的输入,并将其附加到不同的位置。

v9tzhpje

v9tzhpje1#

您没有处理非POST的请求(可能是缩进问题)。您可以像这样更改代码:

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

bt1cpqcv2#

您的data_entry视图没有返回Http对象。您需要确保每个可能的情况都有一些HttpResponseredirect在该视图中。

相关问题