我有两个视图,一个是标签,另一个是类别。即tags字段有ManyToManyField,categories与我的post模型有ForeginKey关系。我的两个观点在这里:
def tag_posts(request, slug):
tag = get_object_or_404(Tags, slug=slug)
posts = tag.post_set.all()
return render(request, 'blog/cat_and_tag.html', {'posts': posts})
def category_posts(request, slug):
category = get_object_or_404(Category, slug=slug)
posts = category.post_set.all()
return render(request, 'blog/cat_and_tag.html', {'posts': posts})
我想要这个
def new_view(request, slug):
instance = get_object_or_404([I want model name dynamic], slug=slug)
posts = instance.post_set.all()
return render(request, 'blog/cat_and_tag.html', {'posts': posts})
有没有办法把这两种观点合并成一种观点?我读了关于contenttypes框架的文章。我能用这个做我想做的事吗?
1条答案
按热度按时间xqkwcwgp1#
你可以通过一些url重路由魔法来实现这一点。这是你的观点:
在
urls.py
中:现在你可以像这样在模板代码中使用新的URL:
或