python - Django variable from Model not outputting value in template -
the variable {{ post }} gives value in home page, , allows me redirect post page proper id; however, page redirected not display value {{ post }}. i'm not sure issue is, tried change multitude of things, can't seem find problem. how can variable {{ post }} give output on other pages?
here index.html, , articles page:
{% if news_posts %} <div class="row"> <div class="large-8 small-12 large-centered column" id="articles"> <ul class="article_posts"> {% post in news_posts %} <li id="article_title"><a href="{% url 'articles:post' post.id %}">{{ post.post_title }}</a></li> <li id="article_date">posted {{ post.post_date|timesince }} ago</li> <div id="arrow"></div> <hr> <li id="article_image"><img src="static/media/{{ post.post_image }}" /></li> <li id="article_shortdesc">{{ post.post_short_description }}</br><a href="{% url 'articles:post' post.id %}">read more>>></a></li> <hr> {% endfor %} </ul> </div> </div> {% endif %}
the articles template page:
<div class="row"> <div class="large-8 small-12 large-centered column" id="articles"> <ul class="article_posts"> <li id="article_title">{{ post.post_title }}</li> <li id="article_date">{{ post.post_date }}</li> <hr> <li id="article_image"><img src="static/media/{{ post.post_image }}" /></li> <li id="article_shortdesc">{{ post.post }}</li> <hr> </ul> </div> </div>
views.py
from django.shortcuts import render, render_to_response articles.models import newspost django.views import generic articles.forms import requesteditor django.core.context_processors import csrf class postlist(generic.listview): template_name = 'articles/index.html' context_object_name = "news_posts" paginate_by = 5 def get_queryset(self): return newspost.objects.order_by('-post_date')[:5] class postdetail(generic.detailview): model = newspost template_name = 'articles/articles.html'
the apps urls.py
from django.conf.urls import patterns, include, url articles import views urlpatterns = patterns('', url(r'^$', views.postlist.as_view(), name='index'), url(r'^(?p<pk>\d+)/$', views.postdetail.as_view(), name='post'), url(r'^editors_request_form/$', views.editorsrequestform, name='editors'), )
project urls.py
from django.conf.urls import patterns, include, url django.contrib import admin urlpatterns = patterns('', # examples: # url(r'^blog/', include('blog.urls')), url(r'^', include('articles.urls', namespace="articles")), url(r'^editor_login/', include(admin.site.urls)), )
you need add context_object_name='post'
in postdetailview
otherwise, default template variable name used object
.
Comments
Post a Comment