Having trouble saving both the user to the built-in auth_user django model and my custom user model. My code saves the user to the django model but not my Account model.
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' )
and 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)
})
This is the guide I have been following: https://simpleisbetterthancomplex.com/tutorial/2017/02/18/how-to-create-user-sign-up-view.html
EDIT: With some changes( i updated the code above ) I now get the user id in my account table but the attributes I don't. So I'm guessing it has something to do with my register method.
you put at folder:
static/myAccount/register.html
return render(request, 'myAccount/register.html', {'form': form})
i need you'r path?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.