Home > Blockchain >  Django Form inputs show up as blank in Django Admin
Django Form inputs show up as blank in Django Admin

Time:10-27

So I'm creating software for an ice cream company, and I'd like to get the customer's information from the order entry HTML. But when I fill out the first name, last name, shipping address, etc, it shows up in Django admin as blank. Here's my code:

forms.py

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

class customerForm(forms.ModelForm):

    firstName = forms.CharField(max_length=30)
    lastName = forms.CharField(max_length=30)
    shippingAddress = forms.CharField(max_length=60)
    billingAddress = forms.CharField(max_length=60)

    class Meta:
        model = customerInfo
        fields = ('firstName','lastName','shippingAddress', 'billingAddress',)

views.py

from django.http import HttpResponse
import orderentry
from orderentry.forms import customerForm

def getCustomerInfo(request):
    form = customerForm(request.POST)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            orderentry.forms.firstName = form.cleaned_data['firstName']
            orderentry.forms.lastName = form.cleaned_data['lastName']
            orderentry.forms.shippingAddress = form.cleaned_data['shippingAddress']
            orderentry.forms.billingAddress = form.cleaned_data['billingAddress']
            return redirect('/orderentry')

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

orderentry.html

<p>
         <!--Basic Customer Information--> 

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

</p>

models.py

from django.db import models
from inventory.models import item, sizeCounts
import uuid

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

    customer_first_name = models.CharField(blank=True, max_length=30)
    customer_last_name = models.CharField(blank=True, max_length=30)
    shipping_address = models.CharField(max_length=60)
    billing_address = models.CharField(max_length=60)
    customer_status_choices = [('PREFFERED','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)

Here's what it looks like in Admin

enter image description here

It is blank in the python shell as well.

I'm new to Django. Any help is appreciated. Thanks.

CodePudding user response:

I can't comment yet so I will post my thoughts here. I think the error may be in your form with your fields variable. I think that the form field names need to match the model's attribute names, otherwise it won't pair together the value from the form with the model attribute. I could be wrong, but that is what I think. So maybe try to match up your form field names with the names of the fields in your model.

  • Related