0

The variable {{ post }} gives a value in my home page, and also allows me to redirect to a post page with the proper ID; however, the page that it is redirected to does not display any value for {{ post }}. I'm not sure what the issue is, I tried to change a multitude of things, I just can't seem to find the problem. How can I get the variable {{ post }} to give an output on other pages?

Here is my index.html, and the articles page:

{% if news_posts %}
<div class="row">
    <div class="large-8 small-12 large-centered column" id="articles">
        <ul class="article_posts">
            {% for 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
from articles.models import newspost
from django.views import generic
from articles.forms import RequestEditor
from 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

from 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
from 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)),
)

1 Answer 1

2

You need to add context_object_name='post' in your PostDetailView otherwise, the default template variable name used is object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.