Lots going on here, the last row in inventory.Html is suppose to link to a product page using the id from PRODUCTS in views.py. I am trying to create a page for each product using the ID but the path isn't working.
inventory.html code
{% for i in items %}
<tr>
<td>{{i.Name_of_the_Material_per_specification}}</td>
<td>{{i.Site_Material_Code }}</td>
<td><a class="btn btn-sm btn-info" href="{% url 'receipt' products.id %}">View</a></td>
</tr>
{% endfor %}
RECIEPT.HTML
<div class="col-md">
<div class="card card-body">
{% csrf_token %}
{% for i in products %}
<p>Name: {{products.Name_of_the_Material_per_specification}}</p>
{% endfor %}
</div>
</div>
URLS.PY
from django.contrib import admin
from django.urls import path, include
from inventory import views
urlpatterns = [
path('main', views.inventory, name='inventory'),
path('receipt/<str:pk>/', views.products, name="receipt")
views.py
from django.shortcuts import render, HttpResponse
from django.template import loader
from django.shortcuts import redirect
from .models import *
from .forms import *
# Create your views here.
def inventory(request):
items = materialForm.objects.all()
return render(request, 'website/inventory.html', {'items': items})
def products(request, pk):
products = materialForm.objects.get(id=pk)
context = {'products':products }
return render(request, 'website/receipt.html', context )
models.py
class materialForm(models.Model):
Name_of_the_Material_per_specification = models.CharField(max_length=100, null=True)
def __str__(self):
return self.Name_of_the_Material_per_specification
CodePudding user response:
You are doing this:
<td><a class="btn btn-sm btn-info" href="{% url 'receipt' products.id %}">View</a></td>
However, products.id
is empty or does not exist, so this is maybe what you want:
<td><a class="btn btn-sm btn-info" href="{% url 'receipt' i.id %}">View</a></td>
CodePudding user response:
you are getting [ But now I'm receiving "'materialForm' object is not iterable". ] because in the receipts.html , You are iterating the contents inside the products queryset with i but then inside the loop you are again using the the products queryset's name directly instead of using the created iterator i...
do the changes mentioned in the inventory.html and receipt.html and try again ..,
inventory.html code
{% for i in items %}
<tr>
<td>{{i.Name_of_the_Material_per_specification}}</td>
<td>{{i.Site_Material_Code }}</td>
#change the next line to
<td><a class="btn btn-sm btn-info" href="{% url 'receipt' products.id %}">View</a></td>
#this
<td><a class="btn btn-sm btn-info" href="{% url 'receipt' i.id %}">View</a></td>
</tr>
{% endfor %}
RECIEPT.HTML
<div class="col-md">
<div class="card card-body">
{% csrf_token %}
{% for i in products %}
#change the next line to
<p>Name: {{products.Name_of_the_Material_per_specification}}</p>
#this
<p>Name: {{i.Name_of_the_Material_per_specification}}</p>
{% endfor %}
</div>
</div>
URLS.PY
from django.contrib import admin
from django.urls import path, include
from inventory import views
urlpatterns = [
path('main', views.inventory, name='inventory'),
path('receipt/<str:pk>/', views.products, name="receipt")
views.py
from django.shortcuts import render, HttpResponse
from django.template import loader
from django.shortcuts import redirect
from .models import *
from .forms import *
# Create your views here.
def inventory(request):
items = materialForm.objects.all()
return render(request, 'website/inventory.html', {'items': items})
def products(request, pk):
products = materialForm.objects.get(id=pk)
context = {'products':products }
return render(request, 'website/receipt.html', context )