Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 模型中的Django变量未在模板中输出值_Python_Django - Fatal编程技术网

Python 模型中的Django变量未在模板中输出值

Python 模型中的Django变量未在模板中输出值,python,django,Python,Django,变量{{post}}在我的主页中给出一个值,还允许我重定向到具有正确ID的post页面;但是,重定向到的页面不显示{{post}的任何值。我不确定问题是什么,我试图改变很多事情,但我似乎找不到问题所在。如何让变量{{post}}在其他页面上给出输出 这是我的index.html和文章页面: {% if news_posts %} <div class="row"> <div class="large-8 small-12 large-centered column" i

变量{{post}}在我的主页中给出一个值,还允许我重定向到具有正确ID的post页面;但是,重定向到的页面不显示{{post}的任何值。我不确定问题是什么,我试图改变很多事情,但我似乎找不到问题所在。如何让变量{{post}}在其他页面上给出输出

这是我的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 %}
应用程序URL.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'),
)

您需要在
PostDetailView
中添加
context\u object\u name='post'
,否则,使用的默认模板变量名为
object

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