![](/img/trans.png)
[英]How to add new fields in registration form using django-registration
[英]Using a registration form to add to custom user fields (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字段注冊到數據庫,但沒有添加的自定義字段保存在可用列中。 任何幫助都會很棒,這真的讓我煩惱。 下面是一片空曠的田野,干杯!
您正在保存僅用戶user.save()
其他Profile
(請將您的user
表更改為有意義的Profile
。
此外,您的個人資料中也不需要password
first_name
last_name
email
。
我建議使用ModelForm
來保存Profile表。
user_form = CreateAccountForm(request.POST)
profile_form = ProfileForm(request.POST)
if user_form.is_valid() and profile_form.is_valid():
# save the form after your password check
user_form.save()
profile_form.save()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.