简体   繁体   中英

How can I return regular response in override get method, Django

I try to use class based views in Django. And I have such problem: I define a base class for a blog ( BlogBaseView ) and two other classes, that inherit it. And in the second class( BlogIndexView ) I want to make the search by get request, so I have override get method. It works, but if I don't make get request, it returns HttpResponse, however I want to return usual context (which BlogIndexView retunes without override get method).

What can I do?

class BlogBaseView(View):

    def get_context_data(self, **kwargs):
        context = super(BlogBaseView, self).get_context_data(**kwargs)
        blog_categories = []
        categories = BlogCategory.objects.all()
        for category in categories:
            blog_categories.append(tuple([category, category.get_number_of_category_items]))

        context['name_page'] = 'blog'
        context['tags'] = Tag.objects.all()
        context['blog_categories'] = blog_categories
        return context

class BlogIndexView(BlogBaseView, ListView):
    queryset = Post.objects.all().order_by('-date_create')
    template_name = 'index_blog.html'
    context_object_name  = 'posts'

    def get(self, request):
        if request.GET.get('tag'):
            context = {
                'posts' : Post.objects.filter(tags__name__in=[request.GET.get('tag')])
            }
            return render(request, self.template_name, context)
        return HttpResponse('result')

class BlogFullPostView(BlogBaseView, DetailView):
    model = Post
    template_name = 'full_post.html'
    pk_url_kwarg = 'post_id'
    context_object_name = 'post'

Thanks!

ListView class also has a get_context_data method, so you should override that instead of get method. Using super you'll get access to BlogBaseView.get_context_data and then you can extended the result.

Here's how:

class BlogIndexView(BlogBaseView, ListView):
    queryset = Post.objects.all().order_by('-date_create')
    template_name = 'index_blog.html'
    context_object_name  = 'posts'

    def get_context_data(self, **kwargs):
        # use the (super) force Luke
        context = super(BlogIndexView, self).get_context_data(**kwargs)
        if self.request.GET.get('tag'):
            context['posts'] = Post.objects.filter(tags__name__in=[self.request.GET.get('tag')])
        return context

If you are overriding ListView then it's not a good idea to override the get method, as you will lose a lot of the ListView functionality.

In this case, it would be a better idea to override get_queryset , and do the search there.

def get_queryset(self):
    queryset = super(BlogIndexView, self). get_queryset() 
    if request.GET.get('tag'):
        queryset = queryset.filter(tags__name=request.GET['tag'])
    return queryset

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM