Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8103095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:25:55+00:00 2026-06-05T23:25:55+00:00

I’d like to write an except clause that redirects the user if there isn’t

  • 0

I’d like to write an except clause that redirects the user if there isn’t something in a queryset. Any suggestions welcome. I’m a Python noob, which I get is the issue here.

Here is my current code:

def get_queryset(self):
    try:
        var = Model.objects.filter(user=self.request.user, done=False)
    except:
        pass

    return var

I want to do something like this:

def get_queryset(self):
    try:
        var = Model.objects.filter(user=self.request.user, done=False)
    except:
        redirect('add_view')

    return var
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:25:57+00:00Added an answer on June 5, 2026 at 11:25 pm

    A try except block in the get_queryset method isn’t really appropriate. Firstly, Model.objects.filter() won’t raise an exception if the queryset is empty – it just returns an empty queryset. Secondly, the get_queryset method is meant to return a queryset, not an HttpResponse, so if you try to redirect inside that method, you’ll run into problems.

    I think you might find it easier to write a function based view. A first attempt might look like this:

    from django.shortcuts import render
    
    def my_view(request):
        """
        Display all the objects belonging to the user 
        that are not done, or redirect if there are not any,
        """
        objects = Model.objects.filter(user=self.request.user, done=False)
        if not objects:
            return HttpResponseRedirect("/empty-queryset-url/")
        return render(request, 'myapp/template.html', {"objects": objects})
    

    The advantage is that the flow of your function is pretty straight forward. This doesn’t have as many features as the ListView generic class based view (it’s missing pagination for example), but it is pretty clear to anyone reading your code what the view is doing.

    If you really want to use the class based view, you have to dig into the CBV documentation for multiple object mixins and the source code, and find a suitable method to override.

    In this case, you’ll find that the ListView behaviour is quite different to what you want, because it never redirects. It displays an empty page by default, or a 404 page if you set allow_empty = False. I think you would have to override the get method to look something like this (untested).

    class MyView(ListView):
        def get_queryset(self):
            return Model.objects.filter(user=self.request.user, done=False)
    
        def get(self, request, *args, **kwargs):
            self.object_list = self.get_queryset()
    
            if len(self.object_list == 0):
                return HttpResponseRedirect("/empty-queryset-url/")
            context = self.get_context_data(object_list=self.object_list)
            return self.render_to_response(context)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.