Exception handling in Django of queryset
<p>I'm trying to catch an exception if a user does not exist and redirect if that's the case. </p>

<p>When I run this I get an error saying:</p>

<blockquote>
  <p>'NoneType' object is not iterable</p>
</blockquote>

<pre><code>try:
    return {'sub_user': User.objects.get(username=username)}
except User.DoesNotExist:
    redirect('home')
</code></pre>

<p>How can I catch this error? I tried with:</p>

<pre><code>try:
    return {'sub_user': User.objects.get(username=username)}
except User is None:
    redirect('home')
</code></pre>

<p>But that gave me another error:</p>

<blockquote>
  <p>catching classes that do not inherit from BaseException is not allowed</p>
</blockquote>

<p>The problem is not in how you catch the exception.</p>

<p>You need to return the result of the call to <code>redirect</code>.</p>

python django python-3.x