我有两个问题:
编辑:
我做了一些调整并部分解决了这个问题。但是现在,当我提交一个新的博客文章时,它会把我带到错误的网址,无论我尝试什么网址,我都找不到完整的帖子。当我点击指向完整帖子的链接时,我会收到类型错误" Object" Post"是不可迭代的#34;。类别" progresstracker"效果很好," blogtopics /.."没有按'吨。
这是最新的代码:
def pt_detail(request, slug, category):
post = get_object_or_404(Post, slug=slug, category__slug=category)
template = CATEGORY_TEMPLATES.get(post.category.slug)
return render(request, template, {'post': post})
def bt_detail(request, slug, category):
post = get_object_or_404(Post, slug=slug, category__slug=category)
template = CATEGORY_TEMPLATES.get(post.category.slug)
return render(request, template, {'post': post})
def post_new(request):
if request.method == "POST":
form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.published_date = timezone.now()
post.save()
if post.category.slug == 'blog-topics':
return redirect('bt_detail', slug=post.slug, category=post.category)
else:
return redirect('pt_detail', slug=post.slug, category=post.category)
else:
form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form})
def post_edit(request, slug, category):
post = get_object_or_404(Post, slug=slug, category__slug=category)
if request.method == "POST":
form = PostForm(request.POST, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.published_date = timezone.now()
post.save()
if post.category.slug == 'blog-topics':
return redirect('bt_detail', slug=post.slug, category=post.category)
else:
return redirect('pt_detail', slug=post.slug, category=post.category)
else:
form = PostForm(instance=post)
return render(request, 'blog/post_edit.html', {'form': form})
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^home/$', views.home, name='home'),
url(r'^blogtopics/computer-science/$', views.compsci, name='computer-science'),
url(r'^blogtopics/data-science/$', views.datasci, name='data-science'),
url(r'^blogtopics/other/$', views.other, name='other'),
url(r'^blogtopics/$', views.blogtopics, name='blogtopics'),
url(r'^resources/$', views.resources, name='resources'),
url(r'^new/$', views.post_new, name='post_new'),
url(r'^progresstracker/$', views.progresstracker, name='progresstracker'),
url(r'^blogtopics/(?P<category>[\w-]+)/(?P<slug>[\w-]+)/$', views.bt_detail, name='bt_detail'),
url(r'^blogtopics/(?P<category>[\w-]+)/(?P<slug>[\w-]+)/edit/$', views.post_edit, name='post_edit'),
url(r'^(?P<category>[\w-]+)/(?P<slug>[\w-]+)/$', views.pt_detail, name='pt_detail'),
url(r'^(?P<category>[\w-]+)/(?P<slug>[\w-]+)/edit/$', views.post_edit, name='post_edit'),
]
{% extends 'blog/base.html' %}
{% block nav %}
<li><a href="/home">Home</a></li>
<li><a href="/progresstracker">Progress Tracker</a></li>
<li class="dropdown">
<a class="active" href="/blogtopics" class="dropbtn">Blog Topics</a>
<div class="dropdown-content">
<a href="/blogtopics/computer-science">Computer Science</a>
<a href="/blogtopics/data-science">Data Science</a>
<a href="/blogtopics/other">Other</a>
</div>
</li>
<li><a href="/resources">Resources</a></li>
{% endblock %}
{% block content %}
<div id="content">
<div class="padding">
<p style="padding-top: 50px">
<div class="post_list">
{% for post in post %}
<h3><a href="{{ post.get_absolute_url_bt }}">{{ post.title }}</a></h3>
<p>{{ post.published_date }}</p>
{% endfor %}
</div>
</div>
</div>
{% endblock %}
class Post(models.Model):
title = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
body = models.TextField()
posted = models.DateField(db_index=True, auto_now_add=True)
category = models.ForeignKey('books.Category', related_name='%(class)s_slug')
created_date = models.DateTimeField(
default = timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __unicode__(self):
return '%s' % self.title
def get_absolute_url(self):
return "/%s/%s/" % (self.category, self.slug)
def get_absolute_url_bt(self):
return "/blogtopics/%s/%s/" % (self.category, self.slug)
答案 0 :(得分:0)
看起来你在这里发生了一些事情。首先,您说任何类别都会被发送到空白的计算机科学页面。这告诉我,您的if
语句总是导致错误,因此它尝试呈现computerscience.html
模板。 pt_detail
视图在上下文中有一个Post对象(ptpost
)。 compsci
视图使用相同的computerscience.html
模板在上下文中使用posts
呈现Post对象的QuerySet。你不能做到这两点。由于您的模板在您的上下文中期待posts
,并且只找到ptpost
,因此无法呈现任何内容,因此会显示空白页。
至于使用类别来确定要使用的模板,您可以使用一系列elif
语句,或者您可以执行以下操作:
CATEGORY_TEMPLATES = {
'progresstracker': 'blog/pt_detail.html',
'anothercategory': 'blog/ac_detail.html',
'yetanothercategory': 'blog/yac_detail.html',
}
def pt_detail(request, slug, category):
ptpost = get_object_or_404(Post, slug=slug, category__slug=category)
template = CATEGORY_TEMPLATES.get(ptpost.category.slug, 'blog/default_detail.html')
return render(request, template, {'ptpost': ptpost})
这将从CATEGORY_TEMPLATES
词典中选择类别,如果当前类别不在词典中,则使用blog/default_detail.html
作为默认值。