gpt4 book ai didi

javascript - AttributeError at/accounts/regist_save/'User' 对象没有属性 'user'

转载 作者:行者123 更新时间:2023-11-28 03:53:56 25 4
gpt4 key购买 nike

AttributeError at/accounts/regist_save/'User' object has no attribute 'user'发生错误。我在views.py中写道我写在views.py

def regist(request):
regist_form = RegisterForm(request.POST or None)
profile_form = ProfileForm(request.POST or None)
context = {
'regist_form': regist_form,
'profile_form': profile_form,
}
return render(request, 'registration/regist.html', context)

@require_POST
def regist_save(request):

regist_form = RegisterForm(request.POST or None)
profile_form = ProfileForm(request.POST or None)

if request.method == "POST" and regist_form.is_valid() and profile_form.is_valid():
regist = regist_form.save(commit=False)
regist.is_staff = True
regist.save()

profile = profile_form.save(commit=False)
profile.user = regist.user

return render(request, 'registration/detail.html')

在regist.html

<div class="form-group-lg">

<label for="id_username">Username</label>

{{ regist_form.username }}

</div>

<div class="form-group-lg">

<label for="id_email">Email</label>

{{ regist_form.email }}
</div>

<div class="form-group-lg">

<label for="id_password">Password</label>

{{ regist_form.password1 }}

</div>


<div class="form-group-lg">
<label for="id_password">Password2</label>

{{ regist_form.password2 }}
<p class="help-block">{{ regist_form.password2.help_text }}</p>

</div>



{% load static %}

<div class="form-group-lg">
<label for="sex">SEX</label>
<select id="sex" class="form-control sex" name="sex">
<option value="">--</option>
<option value="male">male</option>
<option value="female">female</option>
</select>
</div>


<script src="{% static 'accounts/register.js' %}"></script>


<button type="submit" class="btn-lg regist">REGIST</button>

<input name="next" type="hidden" />

{% csrf_token %}

</form>

在 models.py 中

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

class NewUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
birthday = models.CharField(max_length=100,null=True, blank=True, default=None)
sex = models.CharField(max_length=100,null=True, blank=True, default=None)

我真的不明白为什么会发生这个错误,因为我认为 profile.user = regist.user 可以获取用户。我尝试编写 profile.user = request.user code> 但是发生了 IntegrityError 。我如何获取用户?为什么我无法使用此代码获取它?我是否写错了 User model 的列名?我应该如何解决这个问题?

最佳答案

我认为regist本身就是用户对象。

尝试一下:

profile.user = regist

关于javascript - AttributeError at/accounts/regist_save/'User' 对象没有属性 'user',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47734706/

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