4️⃣ 🛠️ Django Admin Interface
Estimated reading: 3 minutes 29 views

👤 Django Create User – Add Users via Shell, Admin, and Views (2025 Guide)

🧲 Introduction – What Does “Create User” Mean in Django?

Creating users in Django allows you to manage authentication, permissions, and user-specific data in your app. Django’s built-in User model supports everything from logins to profile management. You can create users through the admin panel, command line, or programmatically via views.

🎯 In this guide, you’ll learn:

  • How to create users using Django shell
  • How to create users from views
  • How to create superusers
  • Best practices for user security

🧱 Step 1: Create a Superuser for Admin Access

Superusers can access the Django Admin and manage all records.

$ python manage.py createsuperuser

Enter:

  • Username
  • Email
  • Password

✅ This user has full access to admin and all models.


🧪 Step 2: Create a User Using Django Shell

$ python manage.py shell

Then:

from django.contrib.auth.models import User
user = User.objects.create_user(username='john_doe', email='john@example.com', password='securepassword123')

✅ This creates a regular authenticated user.


🌐 Step 3: Create User from a View (Not Recommended for Public Use Without CSRF)

views.py

from django.contrib.auth.models import User
from django.http import HttpResponse

def create_test_user(request):
    User.objects.create_user(username='demo', email='demo@example.com', password='demopassword')
    return HttpResponse("✅ Test user created.")

✅ Useful for testing purposes, but wrap in permission checks for production.


🔐 Step 4: Use Django Admin to Add Users

  1. Start the server: $ python manage.py runserver
  2. Visit:
    🔗 http://localhost:8000/admin/
  3. Navigate to Users → Click Add User
  4. Enter username, password → Save
  5. Assign permissions, groups, and profile info on the next page.

⚙️ Optional: Customize User Model (Advanced)

To customize user fields (like phone number), extend AbstractUser:

from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    phone_number = models.CharField(max_length=20, blank=True)

Update settings.py:

AUTH_USER_MODEL = 'yourapp.CustomUser'

✅ Best Practices

  • Always use create_user() (never User() + save()) to hash passwords
  • Use secure, random passwords for shell-created users
  • Don’t expose user creation via GET requests
  • Use forms and UserCreationForm for registration pages

📌 Summary – Recap & Next Steps

🔍 Key Takeaways:

  • Create superusers for admin using createsuperuser
  • Create users via create_user() in shell or views
  • Admin panel allows full user management
  • Extend AbstractUser for custom user profiles

⚙️ Real-World Relevance:
Users are central to most web applications—auth, permissions, profiles, e-commerce accounts, and admin access all depend on well-managed user creation.


❓ Frequently Asked Questions (FAQ)

❓ What’s the difference between create_user() and create_superuser()?

create_user() → Regular user
create_superuser() → Admin with full access (is_staff=True, is_superuser=True)


❓ Can I manually set a user password?

✅ Yes, use:

user.set_password('newpassword')
user.save()

❓ Can I create users through a form?

✅ Yes. Use UserCreationForm:

from django.contrib.auth.forms import UserCreationForm

❓ How do I check if a user exists?

User.objects.filter(username='john_doe').exists()

❓ How do I activate or deactivate a user?

✅ Use:

user.is_active = False
user.save()

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Django Create User

Or Copy Link

CONTENTS
Scroll to Top