python - Acess Foreign key on django template from queryset values -
i can't seem find i'm doing wrong. here's setup
from django.db import models django.conf import settings """ simple model handle blog posts """ class category(models.model): def __unicode__(self): return self.name name = models.charfield(max_length=50) class blogentry(models.model): def __unicode__(self): return self.blog_post_name blog_post_name = models.charfield(max_length=200) blog_post_content = models.charfield(max_length=1024) blog_pub_date = models.datetimefield(auto_now=true) blog_post_image = models.imagefield(upload_to = settings.media_root, default = '/media_files/douche.jpg') blog_post_category = models.foreignkey(category) views.py django.shortcuts import render blog_post.models import blogentry, category blog_post.forms import blogpostform django.shortcuts import httpresponseredirect time import gmtime, strftime """ method display user posts (newest first) """ def home(request): blog_template = "blogs.html" list_all_posts = blogentry.objects.all().order_by('-blog_pub_date').values() # list posts on db ordered date (newest first). return render(request, blog_template, locals())
on template can access value of blog_post_category_id show actual name of category.
i've tried on template set.all
{% extends "base.html" %} {% load split_string %} {% block content %} <div class="row"> <div class="large-6 columns"> {% if list_all_posts %} {% post in list_all_posts %} <ul class="pricing-table"> <li class="title">{{ post.blog_post_name }}</li> <li class="bullet-item"> <a class="fancybox" href="/media_files/{{ post.blog_post_image|img_path_last_value:'/' }}" title="{{ post.blog_post_content }}" > <img src="/media_files/{{ post.blog_post_image|img_path_last_value:'/' }}" alt="" /> </a> </li> {% cat in post.category_set.all %} <li class="price">{{ cat.category_name }}</li> {% endfor %} <li class="description">{{ post.blog_post_content}}</li> </ul> {% endfor %} {% endif %} </div> </div> {% endblock %}
can shed light on this?
thanks in advance.
from docs
.values() returns valuesqueryset — queryset subclass returns dictionaries when used iterable, rather model-instance objects.
this means not able call post.category_set.all
because post dictionary not model instance, need specify fields included in dictionary , when include blog_post_category__name
show category name in template.
Comments
Post a Comment