我有一個註冊表單,可以在視圖中生成。現在我需要添加其他模型的一些字段。我應該如何更改視圖以添加來自其他模型的字段?如何添加額外的字段到Django註冊表單?
這裏是我的視圖代碼:
def register(request):
""" User registration """
if auth.get_user(request).username:
return redirect('/')
context = {}
context.update(csrf(request))
context['form'] = UserCreationForm()
if request.POST:
newuser_form = UserCreationForm(request.POST)
if newuser_form.is_valid():
newuser_form.save()
newuser = auth.authenticate(username=newuser_form.cleaned_data['username'],
password=newuser_form.cleaned_data['password2'])
auth.login(request, newuser)
return redirect('/')
else:
context['form'] = newuser_form
return render(request, 'user_auth/user_auth_register.html', context)
嘗試繼承'UserCreationForm' – bakatrouble
也許這將是爲別人 https://simpleisbetterthancomplex.com/tutorial/2017/02/18/how-to-create-user-sign-up-view.html –