I want to make it such that if the user has blocked me or if I have blocked the user, I want to redirect them back to the homepage and not allow them to view the detail page. Because this is a class based view, do you have any ways for me to achieve what I want and not affecting what already exists?
I tried to do all sorts of things but didn't work, and none of my friends could solve this. I cannot directly use return redirect(HomeFeed:main) because I have other contexts in the same view which I need it to return in the template.
I also do not want to use UserMixin's test_funct() which involves showing a 403 Forbidden Error because it isn’t user friendly and doesn’t show the user what exactly is happening. That’s why I want to do a redirect followed by django messages to inform them why they can’t view the page
class DetailBlogPostView(BlogPostMixin,DetailView):
template_name = 'HomeFeed/detail_blog.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
blog_post=self.get_object()
blog_post.save()
context['blog_post'] = blog_post
account = Account.objects.all()
context['account'] = account
if blog_post.interest_set.exists():
context["interest_pk"]=blog_post.interest_set.first().pk
if blog_post.author in self.request.user.blocked_users.all():
messages.warning(self.request, 'You cannot view post of ideas authors that you have blocked.', extra_tags='blockedposts')
hi = redirect('HomeFeed:main')
context['hi'] = hi
if blog_post.author in self.request.user.blocked_users.all():
messages.warning(self.request, 'You cannot view post of ideas authors that have blocked you.', extra_tags='blockeduposts')
hi = redirect('HomeFeed:main')
context['bye'] = bye
return context
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…