Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.8k views
in Technique[技术] by (71.8m points)

django - TypeError: context must be a dict rather than HttpResponseRedirect: Is it really impossible to redirect to a url without using test_func()?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do that in get method:

from django.views.generic import DetailView
from django.shortcuts import redirect

class MyDetailView(DetailView):
    ...

    def get(self, request, *args, **kwargs):
        self.object = blog_post = self.get_object()
        blog_post.save()
        if blog_post.author in request.user.blocked_users.all():
            messages.warning(request, 'You cannot view post of ideas authors that you have blocked.', extra_tags='blockedposts')
            return redirect('HomeFeed:main')
        context = self.get_context_data(object=self.object)
        return self.render_to_response(context)
    ...

UPDATE

YOUR VIEW:

class DetailBlogPostView(BlogPostMixin,DetailView):
    template_name = 'HomeFeed/detail_blog.html'

    def get(self, request, *args, **kwargs):
        self.object = blog_post = self.get_object()
        blog_post.save()
        if blog_post.author in request.user.blocked_users.all():
            messages.warning(request, 'You cannot view post of ideas authors that you have blocked.', extra_tags='blockedposts')
            return redirect('HomeFeed:main')
        context = self.get_context_data(object=self.object)
        return self.render_to_response(context)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['blog_post'] = self.object
        context['account'] = Account.objects.all()

        if self.object.interest_set.exists():
            context["interest_pk"] = self.object.interest_set.first().pk
        
        return context

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...