我正在将WordPress网站转换为django网站。我需要保留旧帖子的网址结构,但新帖子的结构不同。我通过创建2个网址,在settings.py中设置日期,然后像这样设置绝对网址来完成此操作:
urls.py
url(r'^reviews/archives/(?P<pk>\d+)$', PostDetail.as_view(), name="oldpost_view"),
文章/ urls.py
url(r'^(?P<slug>[-\w]+)$', PostDetail.as_view(), name="post_view"),
文章/ models.py
@property
def is_old_post(self):
wp_date = settings.WP_ARCHIVE_DATE
if self.post_date.date() < wp_date:
return True
# return False
@models.permalink
def get_abs_url(self):
if self.is_old_post:
return ('oldpost_view', (), {
'pk': self.id,
}
)
else:
return ('post_view', [str(self.url_slug)])
我正在为2个网址使用一个视图:
class PostDetail(DetailView):
model = Post
slug_field = 'url_slug'
template_name = "posts/detail.html"
这一切都很棒。现在,我需要的是阻止oldpost_view url呈现新帖子,反之亦然。我知道我可以覆盖“获取”并使用反向,但我如何判断请求来自哪个网址?什么是最有效和干燥的方法呢?
答案 0 :(得分:1)
如果您不按照我上面的“301”状态代码的建议,请按以下步骤操作:
大致类似的东西。
答案 1 :(得分:0)
根据Issac Kelly的反馈,我能够解决我的问题。这是更新后的观点:
class PostDetail(DetailView):
model = Post
slug_field = 'post_name'
template_name = "posts/detail.html"
def OldPostView(request, pk):
post_name = get_object_or_404(Post, pk=pk).post_name
return redirect('post_view', slug=post_name, permanent=True)
我还更新了我的模型以利用WordPress的“post_name”字段,然后简化了我的永久链接:
@models.permalink
def get_abs_url(self):
return ('post_view', [str(self.post_name)])
谢谢Issac!