CRUD Application - ( User Management in Django )
Heading h2
Syntax
Create User
from django.contrib.auth.models import User
user = User.objects.create_user('username', 'email', 'password')
user.first_name = 'first_name'
user.last_name = 'last_name'
user.save()
Read User
from django.contrib.auth.models import User
users = User.objects.all()
Update User
from django.contrib.auth.models import User
user = User.objects.get(id=1)
user.first_name = 'new_first_name'
user.last_name = 'new_last_name'
user.save()
Delete User
from django.contrib.auth.models import User
user = User.objects.get(id=1)
user.delete()
Example
User Model
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone_number = models.CharField(max_length=10)
address = models.CharField(max_length=100)
def __str__(self):
return self.user.username
User Registration
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect('home')
else:
form = UserCreationForm()
context = {'form': form}
return render(request, 'registration/register.html', context)
User Update
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .forms import UserUpdateForm, ProfileUpdateForm
@login_required
def profile(request):
if request.method == 'POST':
u_form = UserUpdateForm(request.POST, instance=request.user)
p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(request, 'Your account has been updated!')
return redirect('profile')
else:
u_form = UserUpdateForm(instance=request.user)
p_form = ProfileUpdateForm(instance=request.user.profile)
context = {'u_form': u_form, 'p_form': p_form}
return render(request, 'users/profile.html', context)
Output
User Registration
After submitting the registration form, a new user will be added to the Django authentication system.
User Update
After submitting the update form, the user's profile details will be updated in the Django authentication system.
Explanation
A CRUD (Create, Read, Update, Delete) application in Django can be used to manage user accounts. The Django authentication system provides built-in models and views for user management. A user model can be extended to include additional fields using a OneToOneField.
- Create: To create a user, use the User.objects.create_user() method or the built-in UserCreationForm.
- Read: To read users, use the User.objects.all() method to fetch all users or use a queryset to fetch specific users.
- Update: To update a user, use the User.objects.get() method to fetch the user and update its attributes.
- Delete: To delete a user, use the User.objects.get() method to fetch the user and delete it.
Use
This CRUD application can be used to manage user accounts in a Django web application. It can handle user registration, login, password reset, user profile update, and more.
Important Points
- The Django authentication system provides built-in models and views for user management
- A user model can be extended to include additional fields using a OneToOneField
- To create a user, use the User.objects.create_user() method or the built-in UserCreationForm
- To read users, use the User.objects.all() method or a queryset to fetch specific users
- To update a user, use the User.objects.get() method to fetch the user and update its attributes
- To delete a user, use the User.objects.get() method to fetch the user and delete it
Summary
In summary, this CRUD application in Django can manage user accounts, including registration, login, password reset, user profile update, and more. The Django authentication system provides built-in models and views for user management, and the User model can be extended to include additional fields using a OneToOneField. The application can handle all aspects of user management in a Django web application.