我正在使用django-allauth进行用户注册和登录,但是我只有一个名为“单击完成”的步骤,当用户注册时,这似乎应该完成他们的个人资料。但是我得到以下错误。 首先,到目前为止,我在这里已经尝试过
model.py
class CustomUser(AbstractUser):
def __str__(self):
return self.email
class Profile(models.Model):
user = models.OneToOneField('users.CustomUser', on_delete=models.CASCADE)
def __str__(self):
return self.user.email
forms.py
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm):
model = CustomUser
fields = ('username', 'email')
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = ('username', 'email')
class ProfileForm(forms.ModelForm):
company_name = forms.CharField(max_length=100)
phone_number = forms.CharField(max_length=20)
city = forms.CharField(max_length=15)
address = forms.CharField(max_length=255)
class Meta:
model = Profile
fields = ['company_name', 'phone_number', 'city', 'address']
views.py
def profile(request):
if request.method == 'POST':
form = ProfileForm(request.POST)
if form.is_valid():
form.save()
redirect('dashboard')
else:
form = ProfileForm()
return render(request, 'users/profile.html', {'form':form})
我收到此错误
Traceback (most recent call last):
File "C:\Users\Zako5\.virtualenvs\inventory_project-V0PloAkR\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Zako5\.virtualenvs\inventory_project-V0PloAkR\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Zako5\.virtualenvs\inventory_project-V0PloAkR\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Zako5\inventory_project\users\views.py", line 13, in profile
form.save()
File "C:\Users\Zako5\.virtualenvs\inventory_project-V0PloAkR\lib\site-packages\django\forms\models.py", line 458, in save
self.instance.save()
File "C:\Users\Zako5\.virtualenvs\inventory_project-V0PloAkR\lib\site-packages\django\db\backends\utils.py", line 99, in execute
return super().execute(sql, params)
File "C:\Users\Zako5\.virtualenvs\inventory_project-V0PloAkR\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Zako5\.virtualenvs\inventory_project-V0PloAkR\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: users_profile.user_id
[06/Apr/2019 11:50:13] "POST /account/profile HTTP/1.1" 500 150456
我已经搜索了一些答案,但是在我的情况下,没有一个有效。 任何帮助将不胜感激!
答案 0 :(得分:2)
您需要在Profile对象中添加user
字段的值。
为此,您可以在保存配置文件之前从request.user
附加用户。使用form.save(commit=False)
将返回尚未保存在数据库中的Profile实例,然后将用户添加到该Profile实例。
例如:
from django.contrib.auth.decorators import login_required
@login_required
def profile(request):
if request.method == 'POST':
form = ProfileForm(request.POST)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
return redirect('dashboard')
else:
form = ProfileForm()
return render(request, 'users/profile.html', {'form':form})