我已经使用django用户模型创建了模型。我可以对创建的表单执行编辑功能。现在,我想使用DELETE按钮删除特定的用户详细信息。这是我的代码
models.py
from django.db import models
from django.contrib.auth.models import User
class UserProfileInfo(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
def __str__(self):
return self.user.username
forms.py
class EditProfileForm(forms.ModelForm):
class Meta():
model = User
fields = ['first_name','last_name','username','email','password']
help_texts={
'username': None
}
views.py
def delete_profile(request, id):
profile = UserProfileInfo.objects.filter(id=id)
if request.method == 'POST':
profile.delete()
return HttpResponseRedirect(reverse('signupapp:index'))
return render(request, "signupapp/delete_profile.html", {'profile': profile})
delete_profile.html
<form action="" method ="POST">
{% csrf_token %}
<p>Are you sure to delete "{{UserProfileInfo.username}}"?</p>
<input type="submit" value="Delete"/>
</form>
删除按钮链接
index.html
<a href="{% url 'signupapp:delete' request.user.id %}"><input type="button" value="Delete"/></a><br>
运行此代码时,它正在工作。但是我要删除的用户并未在数据库中删除。
任何人都可以帮助我清除它。 在此先感谢。
答案 0 :(得分:1)
您正在使用身份验证用户ID删除用户个人资料ID
尝试
from django.contrib.auth.models import User
def delete_profile(request, id): --> id : is auth user id
user = User.objects.get(id=id)
profile = UserProfileInfo.objects.filter(user=user)
if request.method == 'POST':
profile.delete()
User.objects.filter(id=id).delete()