Python django重定向问题。未正确加载对象
我正在尝试创建一个博客。博客主页将包含关于博客帖子的摘要(按日期排序) 当用户单击主页上博客文章的标题时,该文章的页面应该打开 因此,我试图解决上述问题,并遇到了一些问题 下面是Python django重定向问题。未正确加载对象,python,django,Python,Django,我正在尝试创建一个博客。博客主页将包含关于博客帖子的摘要(按日期排序) 当用户单击主页上博客文章的标题时,该文章的页面应该打开 因此,我试图解决上述问题,并遇到了一些问题 下面是post.html文件的外观(显示完整post的文件): {%extends“base.html”%} {%block content%} 苏赫维尔·诺特拉,{{post.date} {{post.summary | safe | linebreaks}} {%endblock%} 下面是我的应用程序中的url.py文
post.html
文件的外观(显示完整post的文件):
{%extends“base.html”%}
{%block content%}
苏赫维尔·诺特拉,{{post.date}
{{post.summary | safe | linebreaks}}
{%endblock%}
下面是我的应用程序中的url.py文件:
from django.conf.urls import patterns,include ,url
from django.views.generic import ListView, DetailView
from blog.models import blog
urlpatterns = patterns('',
url(r'^$',ListView.as_view(queryset=blog.objects.all().order_by("-date")[:20],template_name="blog.html")),
url(r'^(?P<pk>\d+)$',DetailView.as_view(model = blog,template_name="post.html")),
url(r'^archives$',ListView.as_view(queryset=blog.objects.all().order_by("-date"),template_name="archives.html")),
url(r'^latestnews$',ListView.as_view(queryset=blog.objects.all().order_by("-date")[:10],template_name="archives.html")),
)
从django.conf.url导入模式,包括,url
从django.views.generic导入ListView,DetailView
从blog.models导入blog
urlpatterns=模式(“”,
url(r'^$',ListView.as_view(queryset=blog.objects.all().order_by(“-date”)[:20],template_name=“blog.html”),
url(r'^(?P\d+$),DetailView.as_视图(model=blog,template_name=“post.html”),
url(r“^archives$”,ListView.as_view(queryset=blog.objects.all().order_by(“-date”),template_name=“archives.html”),
url(r“^latestnews$”,ListView.as_view(queryset=blog.objects.all().order_by(“-date”)[:10],template_name=“archives.html”),
)
问题是,当我单击主页上的标题时,新页面会打开,并带有正确的url(127.0.0.1/blog/1---1是post.id)
但是页面是空白的,我在页面上看到的都是…
。这告诉我,{{post.id},{{post.title}}
变量工作不正常
什么原因可能导致此问题?将URL中的上下文对象名称设置为希望对象在模板中具有的名称 例如:
url(r'^(?P\d+)$,DetailView.as_view(model=blog,template_name=“post.html”,context_object_name=“post”),
Django使用名为object
(对于DetailView)或object\u list
(对于ListView)的默认上下文对象,除非另有规定
from django.conf.urls import patterns,include ,url
from django.views.generic import ListView, DetailView
from blog.models import blog
urlpatterns = patterns('',
url(r'^$',ListView.as_view(queryset=blog.objects.all().order_by("-date")[:20],template_name="blog.html")),
url(r'^(?P<pk>\d+)$',DetailView.as_view(model = blog,template_name="post.html")),
url(r'^archives$',ListView.as_view(queryset=blog.objects.all().order_by("-date"),template_name="archives.html")),
url(r'^latestnews$',ListView.as_view(queryset=blog.objects.all().order_by("-date")[:10],template_name="archives.html")),
)
url(r'^(?P<pk>\d+)$',DetailView.as_view(model = blog,template_name="post.html", context_object_name="post")),