raise self.model.DoesNotExist(users.models.Profile.DoesNotExist: 配置文件匹配查询不存在
Posted
技术标签:
【中文标题】raise self.model.DoesNotExist(users.models.Profile.DoesNotExist: 配置文件匹配查询不存在【英文标题】:raise self.model.DoesNotExist( users.models.Profile.DoesNotExist: Profile matching query does not exist 【发布时间】:2022-01-15 21:47:20 【问题描述】:views.py
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm, Profile
def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
messages.success(
request, "Your account has been created! Your ar now able to login.")
return redirect('login')
else:
form = UserRegisterForm()
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)
profile = Profile.objects.get(user = request.user)
p_form = ProfileUpdateForm(request.POST, request.FILES, instance = 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)
profile = Profile.objects.get(user = request.user)
p_form = ProfileUpdateForm(request.POST, request.FILES, instance = profile)
context =
'u_form': u_form,
'p_form': p_form
return render(request, 'users/profile.html', context)
signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import CustomUser
@receiver(post_save, sender=CustomUser)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(email=instance)
@receiver(post_save, sender=CustomUser)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
models.py
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm, Profile
def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
email = form.cleaned_data.get('email')
messages.success(
request, "Your account has been created! Your ar now able to login.")
return redirect('login')
else:
form = UserRegisterForm()
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)
prf = Profile.objects.get(user = request.user)
p_form = ProfileUpdateForm(request.POST, request.FILES, instance = prf)
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)
prf = Profile.objects.get(user = request.user)
p_form = ProfileUpdateForm(request.POST, request.FILES, instance = prf)
context =
'u_form': u_form,
'p_form': p_form
return render(request, 'users/profile.html', context)
forms.py
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import Profile
from .models import CustomUser
from django import forms
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = CustomUser
fields = ['name', 'email','registration', 'password1', 'password2']
class UserUpdateForm(forms.ModelForm):
email = forms.EmailField()
class Meta:
model = CustomUser
fields = ['name', 'email','registration']
class ProfileUpdateForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['image']
admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import Profile
from .models import CustomUser
class ProfileAdmin(admin.ModelAdmin):
list_display = ('id', 'user')
list_display_links = ('id', 'user')
list_filter = ('user', )
list_per_page = 20
admin.site.register(Profile, ProfileAdmin)
这里我使用 AbstractBaseUser 扩展用户模型。 我想添加一个新的个人资料。 我正在使用 OneToOneField 添加带有 CustomUser 模型的配置文件模型。 我的注册和登录工作正常。 但看不到配置文件,它给出了错误 但是我正面临上述问题,请帮助我!
【问题讨论】:
分享您的模型和完整的错误回溯 错误回溯粘贴在此链接中。 pastebin.com/dUvWtxS4 这是导致行prf = Profile.objects.get(user = request.user)
的错误,顺便说一句,我在共享代码中没有看到任何变量 prf
?
我认为因为 profile 是函数的名称。也许它可能导致错误。所以将个人资料更改为 prf。
然后也更新帖子..
【参考方案1】:
解决了。
我在signals.py 中遇到Profile.objects.create
的问题
它是从apps.py 导入的。
在 settings.py 安装的应用程序中,我直接包含了我的appname
。
通过写'users'
。
将用户更改为'users.apps.UsersConfig'
解决了这个错误。
【讨论】:
以上是关于raise self.model.DoesNotExist(users.models.Profile.DoesNotExist: 配置文件匹配查询不存在的主要内容,如果未能解决你的问题,请参考以下文章