python - How Can I Access The Primary Key (from the database) In A Template? -
in project, want display category page, lists of products in specific category. each of listings on category page link visitor specific product.
the product view working properly. products accessed url: /prod/999 999 django created record number.
for category page, have data , i'm working on template. can access product name, description etc.. in template in creating link product detail page, need product record. unfortunately, doesn't seem passed template.
how pass record number "pk" template?
here's view code:
from get_data.models import shareasale_data, categorysummed
class categoryview(generic.listview):
model = shareasale_data template_name = 'summary_page/category.html' context_object_name="the_records" paginate_by = 10 def get_queryset(self, **kwargs): # first actual category category db. cat_summed = categorysummed.objects.get(category_url = self.kwargs['s_cat']) # use category previous line fetch product records return shareasale_data.objects.filter(merch_category = cat_summed.category )
not template far. i'm trying access pk:
{% x in the_records %} <h3><a href='/prod/'{{x.id}}>{{x.name}}</a></h3> {% endfor %}
you've put template variable outside closing quote in href attribute, won't used in link browser. put inside.
<a href='/prod/{{x.id}}'>{{x.name}}</a> ^ here
but note isn't how should create links in templates; should use url
tag. assuming detail url defined this:
url(r'^prod/(?p<id>\d+)/$', views.detail, name='my_detail_view')
you do:
<a href='{% url 'my_detail_view' id=x.id %}'>{{x.name}}</a>
Comments
Post a Comment