Jacob Le
Jacob Le

Reputation: 3

Django form input not showing on frontend/html

I'm working with django forms to create a software for an ice cream company and I'm having trouble with my django forms to show up on the front end of my website. I was able to have customer information show up but the customer's order won't show and I am confused what's going on.

orderentry.html

             <!--Basic Customer Information--> 
            <form method = "post">
                {% csrf_token %}
                {{ form.as_p }}
                {{ newOrder}}
                <button type = "submit">Place your order!</button> 
            </form>

views.py

from django.http.response import HttpResponseRedirect
from django.shortcuts import render, redirect
from django.http import HttpResponse
import orderentry
from orderentry.forms import customerForm, customerInformation

def getCustomerInfo(request):
    form = customerForm(request.POST)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            orderentry.forms.customer_first_name = form.cleaned_data['customer_first_name']
            orderentry.forms.customer_last_name = form.cleaned_data['customer_last_name']
            orderentry.forms.shipping_address = form.cleaned_data['shipping_address']
            orderentry.forms.billing_address = form.cleaned_data['billing_address']
            return redirect('/orderentry')

    else:
        form=customerForm()
    
    return render(request, 'orderentry.html', {'form' : form})

def getCustomerOrder(request):
    newOrder = customerInformation(request.POST)
    if request.method == 'POST':
        if newOrder.is_valid():
            newOrder.save()
            #orderentry.forms.order_Item_Flavor = newOrder.cleaned_data['order_Item_Flavor']
            orderentry.forms.half_Pint_Count = newOrder.cleaned_data['half_Pint_Count']
            orderentry.forms.one_Quart_Count = newOrder.cleaned_data['one_Quart_Count']
            orderentry.forms.pint_Count = newOrder.cleaned_data['pint_Count']
            orderentry.forms.half_Gallon_Count = newOrder.cleaned_data['half_Gallon_Count']
            orderentry.forms.gallon_Count = newOrder.cleaned_data['gallon_Count']
            orderentry.forms.cost = newOrder.cleaned_data['cost']
            return redirect('/orderentry')
        else:
            print("error")

    else:
        newOrder=customerInformation()
    
    return render(request, 'orderentry.html', {'newOrder' : newOrder})

forms.py

from django import forms
from django.forms import ModelForm
from orderentry.models import customerInfo, orderInfo

class customerForm(forms.ModelForm):

    customer_first_name = forms.CharField(max_length=30)
    customer_last_name = forms.CharField(max_length=30)
    shipping_address = forms.CharField(max_length=60)
    billing_address = forms.CharField(max_length=60)

    class Meta:
        model = customerInfo
        fields = ('customer_first_name','customer_last_name','shipping_address', 'billing_address',)

class customerInformation(forms.ModelForm):
    #order_Item_Flavor = forms.MultipleChoiceField(choices=['vanilla','chocolate','strawberry','cookiesncream'])
    half_Pint_Count = forms.IntegerField()
    one_Quart_Count = forms.IntegerField()
    pint_Count = forms.IntegerField()
    half_Gallon_Count = forms.IntegerField()
    gallon_Count = forms.IntegerField()
    cost = forms.IntegerField()

    class Meta:
        model = orderInfo
        fields = ('half_Pint_Count', 'one_Quart_Count', 'pint_Count', 'half_Gallon_Count', 'gallon_Count', 'cost',)

models.py

class customerInfo (models.Model):
    class Meta:
        verbose_name = "Customer Information"
        verbose_name_plural = "Customer Information"

    customer_first_name = models.CharField(max_length=30)
    customer_last_name = models.CharField(max_length=30)
    shipping_address = models.CharField(max_length=60)
    billing_address = models.CharField(max_length=60)
    customer_status_choices = [('PREFERRED', 'preferred'), ('OKAY', 'okay'), ('SHAKY', 'shaky')]
    customer_status = models.CharField(max_length=30, choices = customer_status_choices, default="PREFERRED")

    def __str__(self):
        return '%s %s' % (self.customer_first_name, self.customer_last_name)

class orderInfo (models.Model):
    class Meta:
        verbose_name = "Order Information"
        verbose_name_plural = "Order Information"
    
    order_Item_Flavor = models.ForeignKey('inventory.item', on_delete=models.CASCADE)
    half_Pint_Count = models.IntegerField(default=0)
    one_Quart_Count = models.IntegerField(default=0)
    pint_Count = models.IntegerField(default=0)
    half_Gallon_Count = models.IntegerField(default=0)
    gallon_Count = models.IntegerField(default=0)
    cost = models.IntegerField(default=0)
    customer = models.ForeignKey(customerInfo, on_delete=models.CASCADE, default = 0)

    def __str__(self):
        return '%s, Half Pint: %s, Quart: %s, Pint: %s, Half Gallon: %s, Gallon: %s, $%s' % (self.order_Item_Flavor, 
        self.half_Pint_Count, self.one_Quart_Count, self.pint_Count, self.half_Gallon_Count, self.gallon_Count, 
        self.cost)

urls.py

from django.urls import path
from . import views
urlpatterns = [
    path('', views.getCustomerInfo, name = 'orderentry'),
    path('', views.getCustomerOrder, name = 'orderentry'),

Essentially, this is what it looks like and I am unsure how to get the customerInformation to be shown on the front end. Front End

Thanks in advance for any help or guidance, it is greatly appreciated!

Upvotes: 0

Views: 655

Answers (1)

user15256253
user15256253

Reputation:

You may think getCustomerOrder gets called properly, but it does not get called when you access your orderentry.html (I don't your url for this file). That means newOrder is not passed to the template (your orderentry.html).

So, you have to pass newOrder to your template in getCustomerInfo:

# inside getCustomerInfo func (I omitted other code)
else:
   newOrder=customerInformation() # you used 'newOrder', so I'm using the same name. You can change it, of course.
   form=customerForm()

return render(request, 'orderentry.html', {'form' : form, "newOrder": newOrder}) # notice! you have to put 'newOrder' in this dictionary (or context).

And as of now, your order info does not get saved when user clicks on 'Place your order!' button. So, you have to put the code that saves order after if request.method == 'POST': in getCustomerOrder into getCustomerInfo.

Upvotes: 1

Related Questions