简体   繁体   English

/ accounts / regist_save /'User'对象上的AttributeError没有属性'user'

[英]AttributeError at /accounts/regist_save/ 'User' object has no attribute 'user'

AttributeError at /accounts/regist_save/ 'User' object has no attribute 'user' error happens.I wrote in views.py I wrote in views.py / accounts / regist_save /上的AttributeError'User'对象没有属性'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')

in regist.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>

in models.py 在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)

I really cannot understand why this error happens because I think profile.user = regist.user can be gotten the user.I tried to write profile.user = request.user but IntegrityError happens.How can I get the user?Why can't i get it by using this code?Am I wrong to write User model 's column name?What should i do to fix this? 我真的不明白为什么会发生此错误,因为我认为profile.user = regist.user可以成为用户。我试图编写profile.user = request.user但发生IntegrityError。如何获取用户?我通过使用此代码得到它?我写用户模型的列名是错误的吗?我应该怎么做才能解决这个问题?

I think regist itself is user object. 我认为regist本身就是用户对象。

just try: 你试一试:

profile.user = regist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM