[英]matching query does not exist - Django
I am working on a registration page.我正在注册页面上工作。 I extended django's User model to add additional fields.我扩展了 django 的 User model 以添加额外的字段。 I have two forms connected with OnetoOnefield.我有两个与 OnetoOnefield 连接的 forms。 I am getting this error.我收到这个错误。
DoesNotExist at /register/
Influencer matching query does not exist.
I think what I am doing wrong is creating User and Influencer model at the same time.我认为我做错的是同时创建用户和影响者 model。
My models.py file:我的models.py文件:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Influencer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(blank=True, null=True)
ig_url = models.CharField(max_length=100, blank=True, null=True)
def __str__(self):
return f"{self.user.first_name} {self.user.last_name}"
@receiver(post_save, sender=User)
def create_influencer(sender, instance, created, **kwargs):
if created:
Influencer.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_influencer(sender, instance, **kwargs):
instance.influencer.save()
My forms.py file:我的 forms.py 文件:
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email')
class InfluencerProfileForm(forms.ModelForm):
class Meta:
model = Influencer
fields = ('bio', 'ig_url')
My views.py file:我的views.py文件:
def register(request):
user_form = UserForm()
profile_form = InfluencerProfileForm()
if request.method == 'POST':
user_form = UserForm(request.POST, instance=request.user)
profile = Influencer.objects.get(user=request.user)
profile_form = InfluencerProfileForm(request.POST, instance=profile)
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile_form.save()
messages.success(request, 'Your profile was successfully updated!')
return redirect('settings:profile')
else:
messages.error(request, 'Please correct the error below.')
return render(request, 'accounts/register.html', {
'user_form': user_form,
'profile_form': profile_form
})
I think the problem is in two places.我认为问题出在两个地方。 One, you have a signal which creates Influencer
instance where you al.一,你有一个信号,它会在你所在的地方创建Influencer
实例。 Second, you are assuming you will have a Influencer
instance before creating one.其次,您假设在创建一个 Influencer 实例之前您将拥有一个Influencer
实例。 You can remove the signals and try with the following code:您可以删除信号并尝试使用以下代码:
def register(request):
user_form = UserForm(request.POST or None)
profile_form = InfluencerProfileForm(request.POST or None)
if request.method == 'POST':
if request.user.is_authenticated:
user_form = UserForm(request.POST, instance=request.user)
try:
profile_form = InfluencerProfileForm(request.POST, instance=request.user.influencer) # due to OneToOne relation, it will work
except:
pass
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
messages.success(request, 'Your profile was successfully updated!')
return redirect('settings:profile')
else:
messages.error(request, 'Please correct the error below.')
return render(request, 'accounts/register.html', {
'user_form': user_form,
'profile_form': profile_form
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.