Hi all, I'm having some trouble understanding fairly basic URL mappings and 
how they rout through to templates. I have an app "stock" which is 
registered in my base admin.py. I have three classes in my app, 
stock\models.py listed below:

from django.db import models

    
class Supplier(models.Model):
    supplierName = models.CharField(max_length=64, default='')
    def __str__(self):
        return self.supplierName


class Stock(models.Model):
    stockCode = models.CharField(max_length=32, default='null')
    stock_supplier = models.ManyToManyField(Supplier, through=
'SupplierStock') # Populated automatically
    stockDescription = models.CharField(max_length=128)
    retailPrice = models.CharField(max_length=16, default='')
    
    def __str__(self):
        return self.stockCode + '; ' + self.stockDescription + ' @£' + self.
retailPrice

#Quote        
class SupplierStock(models.Model):
    stock = models.ForeignKey(Stock, on_delete=models.CASCADE)
    supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
    supplierCode = models.CharField(max_length=32)
    supplierPrice = models.CharField(max_length=16)
    quoteRef = models.CharField(max_length=32, default="")
    unitSize = models.PositiveSmallIntegerField(default=1)
    
    def __str__(self):
        return self.supplierCode + ', £' + self.supplierPrice + ' Per ' + 
str(self.unitSize) + '; ' + self.quoteRef
      
The app is a basic stock record for stock items, suppliers, and quotes as 
per "SupplierStock". I am playing around trying to get the individual 
classes in my stock\models.py to map to templates in 
"stock\templates\stock\" but am only having partial success. I can get my 
index.html and details.html to display a list of stock Items, and details 
for the stock items respectfully, but am having trouble displaying similar 
results for the supplier.html template and supplierdetail.html.  Here's my 
code for stock\urls.py

from django.conf.urls import url
from . import views

app_name = 'stock' # Referenced by template, ie <li> item in index.html

urlpatterns = [
    # /stock/
    url(r'^$', views.index, name="index"),
    # /stock/nnn/
    url(r'^(?P<stock_id>[0-9]+)/$', views.detail, name="detail"),
    # /stock/supplier
    url(r'^supplier', views.supplier, name="supplier"),
    # /stock/supplier/nnn/
    url(r'^supplier/(?P<supplier_id>[0-9]+)/$', views.supplierdetail, name=
"supplierdetail"),
]

The stock\views.py code:


from django.shortcuts import render, get_object_or_404
from .models import Stock, Supplier, SupplierStock


def index(request):
    all_stock = Stock.objects.all()
    return render(request, 'stock/index.html', {'all_stock' : all_stock})
    
def detail(request, stock_id):
    stock = get_object_or_404(Stock, id=stock_id)
    return render(request, 'stock/detail.html', {'stock' : stock})
    
def supplier(request):
    all_supplier = Supplier.objects.all()
    return render(request, 'stock/supplier.html', {'all_supplier' : 
all_supplier})
    
def supplierdetail(request, supplier_id):
    supplier = get_object_or_404(Supplier, id=supplier_id)
    return render(request, 'stock/supplierdetail.html', {'supplier' : 
supplier})
    

The code for stock\templates\stock\index.html:

{% if all_stock %}
    <h3>Stock list</h3>
    <ul>
        {% for stock in all_stock %}
        <li><a href="{% url 'stock:detail' stock.id %}">{{ stock.stockCode 
}}; {{ stock.stockDescription }}</a></li>
        {% endfor %}
    </ul>
{% else %}
    <h3>No stock found</h3>
{% endif %}

...and a simple stock\templates\stock\detail.html that shows detail for the 
stock item as expected:

{{ stock }}

I thought I would be able to get away with copying most of the code to 
achieve similar results for the supplier and supplierdetail templates. My 
stock\supplier.html correctly displays a list of links to suppliers, but 
the links do not work, the it just stays on the page. Here's what I have 
for stock\templates\stock\supplier.html

{% if all_supplier %}
    <h3>Supplier list</h3>
    <ul>
        {% for supplier in all_supplier %}
        <li><a href="{% url 'stock:supplierdetail' supplier.id %}">{{ 
supplier.supplierName }}</a></li>
        {% endfor %}
    </ul>
{% else %}
    <h3>No Supplier found</h3>
{% endif %}

...and for stock\templates\stock\supplierdetail.html

<h1>Supplier details here</h1>
{{ supplier }}

As mentioned, the index.html and detail.html templates render fine, but the 
links in supplier.html are dead. I'll be trying to connect the models up in 
one template to display individual quotes from suppliers but am having 
trouble getting past this step. The project is using Django 1.11.8 and 
Python 3.6.2 on Windows 8.1 Pro.

I should also mention that the Django admin page displays all the 
information I'm trying to display fine. Thanks for taking the time to read 
my issue, and I'll be very grateful for any assistance offered!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1ef936f5-18ca-43f6-893d-48fc37e3b909%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to