【问题标题】:Save new user in django user model and custom user model在 django 用户模型和自定义用户模型中保存新用户
【发布时间】:2020-08-23 11:06:10
【问题描述】:

无法将用户保存到内置 auth_user django 模型和我的自定义用户模型。我的代码将用户保存到 django 模型,而不是我的 Account 模型。

models.py

class Account(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
address = models.CharField(max_length=10)
addressNumber = models.CharField(max_length=20)
accountImage = models.CharField(max_length=999)





def create_user_profile(sender, instance, created, **kwargs):
     if created:
           profile, created = 
           Account.objects.get_or_create(user=instance)

post_save.connect(create_user_profile, sender=User)

forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User


class SignUpForm(UserCreationForm):
     address = forms.CharField(max_length=255)
     addressNumber = forms.CharField(max_length=255)
     image = forms.CharField(max_length=255)
     class Meta:
         model = User
         fields = ('username', 'first_name', 'last_name', 'email',
              'password1', 'password2', 'address', 'addressNumber', 'image' )

和views.py

from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from myAccount.forms.userForm import AccountForm
from myAccount.models import Account
from myAccount.forms.forms import SignUpForm


 def register(request):
     form = SignUpForm(data=request.POST)
     if request.method == 'POST':
         if form.is_valid():
             account = form.save()
             account.refresh_from_db()  # load the profile instance created by the signal
             account.address = form.cleaned_data.get('address')
             account.addressNumber = form.cleaned_data.get('addressNumber')
             account.addressImage = form.cleaned_data.get('accountImage')
             account.save()
             raw_password = form.cleaned_data.get('password1')
             return redirect('login')
     return render(request, 'myAccount/register.html', {'form': form})

 @login_required
 def seePurchasehistory(request):
     return render(request, 'myAccount/pruchaseHistory.html')

 @login_required
 def accountInfo(request):
     account = Account.objects.filter(user=request.user).first()
     if request.method == 'POST':
         form = AccountForm(instance=AccountForm, data=request.POST)
         if form.is_valid():
             account = form.save(commit=False)
             account.user = request.user
             account.save()
             return redirect('homepage-index')
     return render(request, 'myAccount/accountInfo.html', {
    'form': AccountForm(instance=account)
})

这是我一直关注的指南:https://simpleisbetterthancomplex.com/tutorial/2017/02/18/how-to-create-user-sign-up-view.html

编辑: 通过一些更改(我更新了上面的代码),我现在在我的帐户表中获得了用户 ID,但我没有获得属性。所以我猜这与我的注册方法有关。

【问题讨论】:

标签: python django django-models django-users


【解决方案1】:

你放在文件夹里:

静态/myAccount/register.html

return render(request, 'myAccount/register.html', {'form': form}) 

我需要你的路径?

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-07-12
  • 2021-12-01
  • 2014-09-03
  • 1970-01-01
  • 2017-08-05
  • 2013-05-12
  • 2020-12-06
相关资源
最近更新 更多