gpt4 book ai didi

python - RelatedObjectDoesNotExist 在/admin/login/User 没有配置文件

转载 作者:行者123 更新时间:2023-12-02 00:29:38 27 4
gpt4 key购买 nike

学习 django 并创建了一个新项目。通过http://127.0.0.1:8000/admin登录管理页面但是在为每个用户添加配置文件代码后,我收到以下错误

错误

Error screenshot

用户/模型.py

from django.db import models
from django.contrib.auth.models import User
from PIL import Image

class Profile(models.Model):
user=models.OneToOneField(User,on_delete=models.CASCADE)
image=models.ImageField(default='default.jpg',upload_to='profile_pics')

def __str__(self):
return f'{self.user.username} Profile'

def save(self):
super().save()
img=Image.open(self.image.path)
if img.height>300 or img.width >300:
output_size=(300,300)
img.thumbnail(output_size)
img.save(self.image.path)

用户/views.py

from django.shortcuts import render,redirect
from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import UserRegisterForm,UserUpdateForm,ProfileUpdateForm

def register(request):
if request.method=="POST":
form=UserRegisterForm(request.POST)
if form.is_valid():
form.save() # it will save user in database
#form.cleaned_data is dictionary it will contain the data from form
username=form.cleaned_data.get('username')
messages.success(request,f'Your account has been created!')
return redirect('login')
else:
form=UserRegisterForm()

# instance of UserCreationForm() class will create form in template as we are passing there
return render(request,'users/register.html',{'form':form})

@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, f'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)

用户/表单.py

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile
class UserRegisterForm(UserCreationForm):
email=forms.EmailField()

class Meta:
model=User #model will affected will be User as after submitting form user table gonna affect. and below will fields in forms
fields=['username','email','password1','password2']

class UserUpdateForm(forms.ModelForm):
email = forms.EmailField()

class Meta:
model = User # model will affected will be User as after submitting form user table gonna affect. and below will fields in forms
fields = ['username', 'email']

class ProfileUpdateForm(forms.ModelForm):
class Meta:
model=Profile
fields=['image']

用户/信号.py

from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import Profile

@receiver(post_save,sender=User)
def create_profile(sender,instance,created,**kwargs):
if created:
Profile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_profile(sender, instance, created, **kwargs):
instance.profile.save()

可能是管理员配置文件不可用且未创建。有什么建议吗?

最佳答案

您需要先创建一个管理员配置文件。由于管理员没有个人资料,因此显示相关对象不存在。我遇到了类似的情况,我创建了一个管理员配置文件。成功了

关于python - RelatedObjectDoesNotExist 在/admin/login/User 没有配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52385596/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com