'I'm trying to input data, save it and display it in my view page. I'm not sure what I'm missing

I'm writing a simple product input app, where a user can input their product details, save it in the database and view it in another page. Everything seems to be going fine, no error messages when I run the server. However, when I put in the product details and submit, the page just refreshes and all the information disappears. Nothing shows up in the product list page. I've read a couple articles but not sure what exactly I'm missing. It seems like I've hit an invisible wall. Please assist. Thank you

forms.py

class ProductForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = ['SKU', 'Category','Name', 'Platform', 'Price','Discount', 'Date','Cost']
        widgets = {
            'SKU': forms.TextInput(attrs={
                'class': 'form-control', 'id': 'SKU'
            }),
            'Category': forms.TextInput(attrs={
                'class': 'form-control', 'id': 'Category'
            }),
             'Name': forms.TextInput(attrs={
                'class': 'form-control', 'id': 'Name'
            }),
             'Platform': forms.TextInput(attrs={
                'class': 'form-control', 'id': 'Platform'
            }),
             'Price': forms.NumberInput(attrs={
                'class': 'form-control', 'id': 'Price'
            }),
            'Discount': forms.NumberInput(attrs={
                'class': 'form-control', 'id': 'Discount'
            }),
            'Date': forms.DateInput(attrs={
                'class': 'form-control', 'id': 'Date'
            }),
            'Cost': forms.NumberInput(attrs={
                'class': 'form-control', 'id': 'Cost'
            }),
        }

views.py

# Product views
@login_required(login_url='login')
def create_product(request):
    forms = ProductForm()
    if request.method == 'POST':
        forms = ProductForm(request.POST)
        if forms.is_valid():
            forms.save()
            return redirect('product-list')
    context = {
        'form': forms
    }
    return render(request, 'store/create_product.html', context)


class ProductListView(ListView):
    model = Product
    template_name = 'store/product_list.html'
    context_object_name = 'product'

models.py

class Product(models.Model):
SKU = models.CharField(max_length=30, unique=True,default='input SKU')
Category = models.CharField(max_length=200,default='Input Category')
Name = models.CharField(max_length=250,unique=True, default='Input product name')
Platform = models.CharField(max_length=50, default='platform')
Price = models.PositiveIntegerField(default='price')
Discount = models.PositiveIntegerField(default='discount')
Date = models.DateTimeField(date, default='Date')
Cost = models.PositiveIntegerField()
created_date = models.DateField(auto_now_add=True)

def __str__(self):
    return self.name

Create produt page html

{% block content %}
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                <strong class="card-title">Create Product</strong>
            </div>
            <div class="card-body">
                <!-- Credit Card -->
                <div id="pay-invoice">
                    <div class="card-body">
                        <form action="#" method="post" novalidate="novalidate">
                            {% csrf_token %}
                            <div class="form-group">
                                <label for="SKU" class="control-label mb-1">SKU</label>
                                {{ form.SKU }}
                            </div>
                            <div class="form-group">
                                <label for="Category" class="control-label mb-1">Category</label>
                                {{ form.Category }}
                            </div>
                            <div class="form-group">
                                <label for="Name" class="control-label mb-1">Name</label>
                                {{ form.Name }}
                            </div>
                            <div class="form-group">
                                <label for="Platform" class="control-label mb-1">Platform</label>
                                {{ form.Platform }}
                            </div>
                            <div class="form-group">
                                <label for="Price" class="control-label mb-1">Price</label>
                                {{ form.Price }}
                            </div>
                            <div class="form-group">
                                <label for="Discount" class="control-label mb-1">Discount Price</label>
                                {{ form.Discount }}
                            </div>
                            <div class="form-group">
                                <label for="Date" class="control-label mb-1">Date</label>
                                {{ form.Date }}
                            </div>
                            <div class="form-group">
                                <label for="Cost" class="control-label mb-1">Cost</label>
                                {{ form.Cost }}
                            </div>
                            <div>
                                <button id="payment-button" type="submit" class="btn btn-lg btn-info btn-block">
                                    <span id="payment-button-amount">Create Product</span>
                                </button>
                            </div>
                        </form>
                    </div>
                </div>

            </div>
        </div> <!-- .card -->

    </div><!--/.col-->
</div>
{% endblock content %}

Product display page

{% block content %}
<div class="row">
    <div class="col-xl-12">
        <div class="card">
            <div class="card-body">
                <h4 class="box-title">Product List </h4>
            </div>
            <div class="card-body--">
                <div class="table-stats order-table ov-h">
                    <table class="table ">
                        <thead>
                            <tr>
                                <th class="serial">#</th>
                                <th>SKU</th>
                                <th>Category</th>
                                <th>Name</th>
                                <th>Platform</th>
                                <th>Price</th>
                                <th>Discount</th>
                                <th>Date</th>
                                <th>Cost</th>
                                <th>Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% if product %}
                            {% for product in product %}
                            <tr>
                                <td class="serial">{{ forloop.counter }}</td>
                                <td>{{ product.SKU }}</td>
                                <td>{{ product.Category }}</td>
                                <td>{{ product.Name }}</td>
                                <td>{{ product.Platform }}</td>
                                <td>{{ product.Price}}</td>
                                <td>{{ product.Discount }}</td>
                                <td>{{ product.Date }}</td>
                                <td>{{ product.Cost }}</td>
                                <td>{{ product.created_date }}</td>
                            </tr>
                            {% endfor %}
                            {% else %}
                                <tr><td>No Product Data</td></tr>
                            {% endif %}
                        </tbody>
                    </table>
                </div> <!-- /.table-stats -->
            </div>
        </div> <!-- /.card -->
    </div>  <!-- /.col-lg-8 -->

    
</div>
{% endblock content %}


Solution 1:[1]

In views.py

from django.http import HttpResponseRedirect
from django.urls import reverse
from django.db import IntegrityError

@login_required(login_url='login')
def create_product(request):
    forms = ProductForm()
    context = {"form": forms}
    if request.method == 'POST':
        # Populate the form with data from post
        forms = ProductForm(request.POST)
        # Update context dictionary
        context['form'] = forms
        # Checking form validity
        if forms.is_valid():
            try:
                forms.save()
                return HttpResponseRedirect(reverse('product-list')
            except IntegrityError:
                print("Print data entry to db failed")
                # Re-render form page with data
                return render(request, 'store/create_product.html', context)                
        # When form is invalid
        else:
            # Remove this after your work is done
            print("Invalid form received")
            # Re-render form page with data
            return render(request, 'store/create_product.html', context)
    else:
        return render(request, 'store/create_product.html', context)

In urls.py

path("something/", ..., name="product-list")

In create product page

...
# Remove anything from action attribute and just put novalidate
<form action="" method="post" novalidate>
...

Solution 2:[2]

The redirect is currently receiving no information, You need to check the form.cleaned_data and provide the appropriate information to the redirect. The redirect doesn't know anything you don't explicitly provide.

return redirect('product-list', product=forms.cleaned_data)

On top of the Product display page expecting a variable called product, you're also overwriting that in the template : {% for product in product %} can lead to errors, change the first term to item or product2 or something else (for item in product ).

This can help if you have further questions: https://realpython.com/django-redirects/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 shah sawood
Solution 2 Lib101