簡體   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