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

  • Home
  • SEARCH
  • 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 168075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:23:20+00:00 2026-05-11T12:23:20+00:00

I’ve got a Django-based site that allows users to register (but requires an admin

  • 0

I’ve got a Django-based site that allows users to register (but requires an admin to approve the account before they can view certain parts of the site). I’m basing it off of django.contrib.auth. I require users to register with an email address from a certain domain name, so I’ve overridden the UserCreationForm‘s save() and clean_email() methods.

My registration page uses the following view. I’m interested in hearing about how I might improve this view—code improvements or process improvements (or anything else, really).

def register(request):     if request.method == 'POST':         form = UserCreationForm(request.POST)          if form.is_valid():             message = None              form.save()              username = form.cleaned_data['username']             password = form.cleaned_data['password1']              user = authenticate(username=username, password=password)              first_name = form.cleaned_data['first_name']             last_name = form.cleaned_data['last_name']             email = user.email              # If valid new user account             if (user is not None) and (user.is_active):                 login(request, user)                 message = '<strong>Congratulations!</strong> You have been registered.'                  # Send emails                 try:                     # Admin email                     pk = None                     try: pk = User.objects.filter(username=username)[0].pk                     except: pass                      admin_email_template = loader.get_template('accounts/email_notify_admin_of_registration.txt')                     admin_email_context = Context({                         'first_name': first_name,                         'last_name': last_name,                         'username': username,                         'email': email,                         'pk': pk,                     })                     admin_email_body = admin_email_template.render(admin_email_context)                     mail_admins('New User Registration', admin_email_body)                      # User email                     user_email_template = loader.get_template('accounts/email_registration_success.txt')                     user_email_context = Context({                         'first_name': form.cleaned_data['first_name'],                         'username': username,                         'password': password,                     })                     user_email_body = user_email_template.render(user_email_context)                     user.email_user('Successfully Registered at example.com', user_email_body)                 except:                     message = 'There was an error sending you the confirmation email. You should still be able to login normally.'             else:                 message = 'There was an error automatically logging you in. Try <a href=\'/login/\'>logging in</a> manually.'              # Display success page             return render_to_response('accounts/register_success.html', {                     'username': username,                     'message': message,                 },                 context_instance=RequestContext(request)             )     else: # If not POST         form = UserCreationForm()      return render_to_response('accounts/register.html', {             'form': form,         },         context_instance=RequestContext(request)     ) 
  • 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. 2026-05-11T12:23:20+00:00Added an answer on May 11, 2026 at 12:23 pm

    You don’t even need this code, but I think the style:

    pk = None try: pk = User.objects.filter(username=username)[0].pk except: pass 

    is more naturally written like:

    try:     user = User.objects.get(username=username) except User.DoesNotExist:     user = None 

    and then in your admin notify template use {{ user.id }} instead of {{ pk }}.

    But, like I said, you don’t need that code at all because you already have a user object from your call to authenticate().

    Generally in Python, it’s considered poor practice to have the exception handler in a try/except block be empty. In other words, always capture a specific exception such as User.DoesNotExist for this case.

    It’s also poor practice to have many lines inside the try part of the try/except block. It is better form to code this way:

    try:     ... a line of code that can generate exceptions to be handled ... except SomeException:     ... handle this particular exception ... else:     ... the rest of the code to execute if there were no exceptions ... 

    A final, minor, recommendation is to not send the email directly in your view because this won’t scale if your site starts to see heavy registration requests. It is better add in the django-mailer app to offload the work into a queue handled by another process.

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

Sidebar

Ask A Question

Stats

  • Questions 164k
  • Answers 164k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The "default format" of a datetime is: ShortDatePattern + '… May 12, 2026 at 12:26 pm
  • Editorial Team
    Editorial Team added an answer Really sorry... looks like it wasn't to do with the… May 12, 2026 at 12:26 pm
  • Editorial Team
    Editorial Team added an answer If you want to use the auto_increment value you can… May 12, 2026 at 12:26 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.