繁体   English   中英

将新用户保存在 django 用户 model 和自定义用户 model

[英]Save new user in django user model and custom user model

无法将用户保存到内置的 auth_user django model 和我的自定义用户 model。 我的代码将用户保存到 django model 但不是我的帐户 model。

模型.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,但我没有的属性。 所以我猜这与我的注册方法有关。

你放在文件夹中:

静态/myAccount/register.html

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

我需要你的路径?

暂无
暂无

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

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