I am converting a WordPress site to a django one. I need to preserve the url structure for old posts, but have a different structure for new posts. I’ve done this by creating the 2 urls, setting a date in settings.py, then setting the absolute url like so:
urls.py
url(r'^reviews/archives/(?P<pk>\d+)$', PostDetail.as_view(), name="oldpost_view"),
posts/urls.py
url(r'^(?P<slug>[-\w]+)$', PostDetail.as_view(), name="post_view"),
posts/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)])
I am using one view for the 2 urls:
class PostDetail(DetailView):
model = Post
slug_field = 'url_slug'
template_name = "posts/detail.html"
This all works great. Now, what I need to is prevent new posts from being rendered by the oldpost_view url and vice versa. I know I can override the “get” and use reverse for this but how can I tell which url the request came from? What is the most efficient and DRY way to do this?
Based on Issac Kelly’s feedback I was able to solve my problem. Here’s the updated views:
I also updated my models to utilize the “post_name” field that WordPress has, then simplified my permalink:
Thanks Issac!