2
我在django中爲註冊表單創建了自定義用戶模型,但我在將數據導入數據庫時遇到了一些問題。目前用戶可以註冊,並且他們的用戶名,密碼,電子郵件,名字和姓氏被保存,但是所有其他表單數據不保存到對象中。下面是我的形式和輸出:使用註冊表單添加到自定義用戶字段(django)
Models.py
class Profile(models.Model):
user = models.OneToOneField(User)
gender = models.CharField(max_length=20)
medication = models.CharField(max_length=50, blank=True)
medical_history = models.CharField(max_length=50,blank=True)
DOB = models.CharField(max_length=20)
telephone = models.CharField(max_length=20)
address = models.CharField(max_length=30)
city = models.CharField(max_length=20)
state = models.CharField(max_length=20)
postcode = models.CharField(max_length=30)
forms.py:
class CreateAccountForm(ModelForm):
class Meta:
model = Profile
fields = ("username","password","password2","first_name","last_name",
"gender","medication","medical_history","DOB","email","telephone",
"address","city","state","postcode")
views.py:
def create_account(request):
form = CreateAccountForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
password2 = form.cleaned_data['password2']
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
gender = form.cleaned_data['gender']
medication = form.cleaned_data['medication']
medical_history = form.cleaned_data['medical_history']
DOB = form.cleaned_data['DOB']
email = form.cleaned_data['email']
telephone = form.cleaned_data['telephone']
address = form.cleaned_data['address']
city = form.cleaned_data['city']
state = form.cleaned_data['state']
postcode = form.cleaned_data['postcode']
if password == password2:
if (password_verification(password)) == 3:
if (username_verification(username)) == False:
form.save()
return HttpResponseRedirect('/login')
else:
return HttpResponseRedirect('/create_account')
else:
return HttpResponseRedirect('/create_account')
else:
return HttpResponseRedirect('/create_account')
return render(request, "create_account.html", {'form': form})
在Django管理窗口,用戶正在使用create_user字段註冊數據庫,但是沒有添加的自定義字段保存在可用的列中納秒。任何幫助將是偉大的,這真的讓我感到困擾。下面是空場的照片,歡呼聲!
我已將模型名稱更改爲Profile,謝謝您的建議。 form.save()會進入視圖文件嗎? – Abjilla22
是的...等待我會改變我的答案一點點,所以你會更好地瞭解... –
@ Abjilla22請看我更新的答案 –