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 7434877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:57:51+00:00 2026-05-29T09:57:51+00:00

I have a function in a Manager which activates a user account via a

  • 0

I have a function in a Manager which activates a user account via a key. When a key is provided, several checks need to be performed: does the key exist? has the key expired? and then if not, the manager activates the account.

def activate(key):
    try:
        profile = self.get(key=key)
    except self.model.DoesNotExist:
        return None

    if not profile.key_expired():
        ## Activate user
        return user

    return None

The issue is of course, that this returns False for both ‘the key doesn’t exist’, and ‘the key given is expired’. Giving False for both doesn’t tell my upstream view what was the issue at hand. I don’t do a 404 error as that is opaque to the user and doesn’t help matters.

What is the best/correct django/pythonic way of handling this to give more useful information upstream? Are custom errors the way forward? Should I return values for analysis upstream (seems an ugly solution)? Is there another way?

  • 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-05-29T09:57:52+00:00Added an answer on May 29, 2026 at 9:57 am

    I’d raise an exception inside activate and catch it outside.

    def activate(key):
        try:
            profile = self.get_query_set().get(key=key)
        except self.model.DoesNotExist:
            raise
    
        if profile.key_expired():
           raise YourCustomException()
           # or simply return False
    
        ## Activate user
        return user
    

    Also I’d suggest to use self.get_query_set().get(key=key) instead of self.get(key=key)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function save() which will send some data to server periodically after
I have written a function which basically hides the complication of using the pySnmp
For example: I have a User which has 10 Widgets. Along with that I
I have the following script which does not work 100%, it returns about 20
I have a form which have five buttons and several checkboxes holding different values.
I have a memory heap manager which partitions the heap into different segments based
I have a person table which holds person and his manager at the same
I have the following code: $(document).ready(function() { // Manage sidebar category display jQuery(#categories >
i have function public Menu Details(int? id) { return _dataContext.Menu.Include(ChildMenu).FirstOrDefault(m => m.MenuId == id);
I have function: char *zap(char *ar) { char pie[100] = INSERT INTO test (nazwa,

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.