2015-04-15 2 views
0

Переменная {{post}} дает значение на моей домашней странице, а также позволяет перенаправить на страницу с соответствующим идентификатором; однако страница, на которую она перенаправлена, не отображает значения для {{post}}. Я не уверен, в чем проблема, я пытался изменить множество вещей, я просто не могу найти проблему. Как получить переменную {{post}} для вывода на другие страницы?Переменная Django из модели не выводит значение в шаблоне

Вот мой index.html, а страница статьи:

{% 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 %} 

страница статьи шаблона:

<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' 

приложения 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'), 
) 

проект 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)), 
) 

ответ

2

Вам нужно добавить context_object_name='post' в вашем PostDetailView иначе, имя переменной шаблона по умолчанию используется object.

+0

Awesome thanks! Это сработало! – ZachPerkitny