I have a Crud Project in which when i add any new member in the list the data i entered is vanished if i entered the same email which is already registered, i want that if i entered the mail id which is already registered so it will only give a message of that ID is already registered and data which i filled in the form it will not vanished , can we do this ?
type here
i want that if i entered the mail id which is already registered so it will only give a message of that ID is already registered and data which i filled in the form it will not vanished , can we do this ?
Add_Member view.py
@login_required
def user_data_create(request):
if request.method == 'POST':
name = request.POST['name']
email = request.POST['email']
phone = request.POST['phone']
gender = request.POST['gender']
role = request.POST['role']
if Members.objects.filter(email=email).exists():
messages.error(request,"This Email Is already registred")
else:
user_data = Members.objects.create(user=request.user, name=name, email=email,
phone=phone, gender=gender,role=role)
user_data.save()
messages.success(request,"New Member added Succesfully")
return redirect('dashboard_page')
return render(request, 'add.html')
Update View.py
@login_required
def user_data_update(request, id):
try:
user_data = Members.objects.get(id=id)
if user_data.user == request.user:
if request.method == 'POST':
user_data.name = request.POST['name']
user_data.email = request.POST['email']
user_data.phone = request.POST['phone']
user_data.gender = request.POST['gender']
user_data.role = request.POST['role']
user_data.save()
messages.info(request,"Member's Details Updated Succesfully")
return redirect('dashboard_page')
return render(request, 'update.html', {'user_data': user_data})
else:
raise Http404("You do not have permission to edit this data.")
except Exception as e:
messages.error(request,"This Email Is already registred ",e)
return render(request,'update.html', {'user_data': user_data})
Add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Members</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"></script>
</head>
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<body>
<div>
{%if messages%}
{% for message in messages%}
<div id="message_container" class="alert alert-warning alert-dismissible fade show text-center" role="alert">
**{{message}}**
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{%endfor%}
{%endif%}
</div>
</div>
<div class="container1">
<h1>Add Member</h1>
<form action="{% url 'user_data_create' %}" method="post">
{% csrf_token %}
<label for="">Name</label><br><br>
<input type="text" name="name" value="{{ user_data.name }}" pattern="^[^ ].+[^ ]$" title="Please Remove Extra Spaces" required><br><br>
<label for="">Email</label><br><br>
<input type="email" name="email" value="{{ user_data.email }}" required><br><br>
<label for="">Contact</label><br><br>
<input type="text" name="phone" value="{{ user_data.phone }}" title="Please enter exactly 10 digits" maxlength="10" minlength="10"
pattern="\d{10}" required><br><br>
<p>Please select Gender:</p>
<input type="radio" name="gender" value="Male">
<label for="male">Male</label><br>
<input type="radio" name="gender" value="Female">
<label for="female">Female</label><br>
<input type="radio" name="gender" value="Other">
<label for="other">Other</label>
<br><br>
<label for="role">Select a Profile :- </label>
<select name="role" id="role">
<option value="None" selected>None</option>
<option value="FM">FM</option>
<option value="TM">TM</option>
<option value="TL">TL</option>
<option value="Traniee" selected>Traniee</option>
</select> <br><br>
<a class="button2" href="{% url 'dashboard_page' %}">
<button type="button" class="btn btn-danger" id="ID">Cancle</button>
</a>
<a class="button2">
<button type="submit" class="btn btn-success" id="ID">Submit<b
I think it's better to use a form.. Maybe something like this:
user_form.py
and in your view pass it to template in the context.
Example for perserving data after submit: In the view, you'll be doing something like this:
view.py
That will save the data after unsuccessful submit but not if you refreseh. To save the data when you refresh, I think you can use some storage like sessions.
Reference:
https://docs.djangoproject.com/en/4.2/ref/forms/ https://docs.djangoproject.com/en/4.2/topics/http/sessions/