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 >
<div >
<div >
<div >
<strong >Create Product</strong>
</div>
<div >
<!-- Credit Card -->
<div id="pay-invoice">
<div >
<form action="#" method="post" novalidate="novalidate">
{% csrf_token %}
<div >
<label for="SKU" >SKU</label>
{{ form.SKU }}
</div>
<div >
<label for="Category" >Category</label>
{{ form.Category }}
</div>
<div >
<label for="Name" >Name</label>
{{ form.Name }}
</div>
<div >
<label for="Platform" >Platform</label>
{{ form.Platform }}
</div>
<div >
<label for="Price" >Price</label>
{{ form.Price }}
</div>
<div >
<label for="Discount" >Discount Price</label>
{{ form.Discount }}
</div>
<div >
<label for="Date" >Date</label>
{{ form.Date }}
</div>
<div >
<label for="Cost" >Cost</label>
{{ form.Cost }}
</div>
<div>
<button id="payment-button" type="submit" >
<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 >
<div >
<div >
<div >
<h4 >Product List </h4>
</div>
<div >
<div >
<table >
<thead>
<tr>
<th >#</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 >{{ 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 %}
CodePudding user response:
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>
...
CodePudding user response:
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/