Вот мой код:
Код: Выделить всё
views.py:Код: Выделить всё
def customerprofile(request):
profile = get_object_or_404(Profile, user_name=request.user)
if request.method == "POST":
print(request.POST) # Check what data is being sent
image = request.FILES.get("image")
full_name = request.POST.get("full_name")
phone = request.POST.get("phone")
address = request.POST.get("address")
country = request.POST.get("country")
golden_user = request.POST.get("golden_user") == 'on'
diamond_user = request.POST.get("diamond_user") == 'on'
if image != None:
profile.image = image
profile.full_name = full_name
profile.phone = phone
profile.address = address
profile.country = country
profile.golden_user = golden_user
profile.diamond_user = diamond_user
try:
profile.save()
print("Profile saved successfully") # Check if the profile was saved
messages.success(request, "Profile Updated Successfully")
return redirect("useradmin:customerprofile")
except Exception as e:
print("Error saving profile:", e) # Check for errors
context = {
"profile":profile,
}
return render(request, 'useradmin/customerprofile.html', context)
Код: Выделить всё
customerprofile.html:Код: Выделить всё
{% load static %}
{% block content %}
Edit Profile
{% csrf_token %}
[img]{% if form.instance.image %}{{ request.user.Profile.image.url }}{% else %}https://static.vecteezy.com/system/resources/thumbnails/009/292/244/small/default-avatar-icon-of-social-media-user-vector.jpg{% endif %}[/img]
Profile Image
Full Name
Phone
Address
Golden User
Diamond User
Verified
Save Changes
.card {
border-radius: 12px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
background: #ffffff;
overflow: hidden;
width: 100%;
padding: 20px;
}
.card-header {
background-color: #333;
color: #fff;
padding: 20px;
border-bottom: none;
border-radius: 12px 12px 0 0;
}
.card-title {
font-size: 2rem;
font-weight: 700;
margin: 0;
}
.card-body {
padding: 30px;
}
.form-group {
margin-bottom: 20px;
}
.form-check {
margin-bottom: 10px;
}
.btn-primary {
background-color: #337ab7;
border-color: #337ab7;
font-size: 1rem;
padding: 10px 20px;
border-radius: 50px;
transition: background-color 0.3s, border-color 0.3s;
}
.btn-primary:hover {
background-color: #23527c;
border-color: #23527c;
}
// Function to preview selected image
function previewImage(event) {
var reader = new FileReader();
reader.onload = function(){
var imagePreview = document.getElementById('imagePreview');
imagePreview.src = reader.result;
};
reader.readAsDataURL(event.target.files[0]);
}
{% endblock %}
Может ли кто-нибудь помочь мне понять, что происходит? ? Заранее спасибо!
Изменить: я добавил проверку ошибок в свою функцию просмотра, и никаких ошибок не получаю. Метод Profile.save() возвращает True, указывая, что сохранение прошло успешно. Но изменения по-прежнему не отражаются в базе данных.
Пожалуйста, помогите мне с этим
Подробнее здесь: https://stackoverflow.com/questions/786 ... -in-django