Templates and URL Mappings

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Templates and URL Mappings

Will Burchell
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 [hidden email].
To post to this group, send email to [hidden email].
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 | Threaded
Open this post in threaded view
|

Re: Templates and URL Mappings

Daniel Roseman-2
On Monday, 2 April 2018 12:00:31 UTC+1, Will Burchell wrote:

<snip>
 
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"),
] 


Firstly, as a clarification, URLs don't map to templates at all; they map to views, and views may or may not render templates. But you do seem to know this.

Your problem however is simply to do with regexes; you don't terminate your supplier pattern, so it matches everything beginning with "supplier". It should be:

    url(r'^supplier$', views.supplier, name="supplier"),

or use the new path syntax in Django 2.0:

    path('supplier', ...)

-- 
DR.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/5b9ca4ab-63e3-4437-aca6-925cbdccd7e9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Templates and URL Mappings

Will Burchell
Daniel, thank you so much!

I had been eyeing my regex with suspicion by was looking at the wrong pattern. Many thanks again, all appeas to be working fine now :)


On Monday, 2 April 2018 12:27:56 UTC+1, Daniel Roseman wrote:
On Monday, 2 April 2018 12:00:31 UTC+1, Will Burchell wrote:

<snip>
 
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"),
] 


Firstly, as a clarification, URLs don't map to templates at all; they map to views, and views may or may not render templates. But you do seem to know this.

Your problem however is simply to do with regexes; you don't terminate your supplier pattern, so it matches everything beginning with "supplier". It should be:

    url(r'^supplier$', views.supplier, name="supplier"),

or use the new path syntax in Django 2.0:

    path('supplier', ...)

-- 
DR.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/29366cc5-07ef-4239-aa1d-648069961eda%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.