我在向数据库添加新的自定义用户时遇到问题。我知道在互联网上有一些关于它的线索,但我无法找到解决问题的方法。点击"提交"注册页面中的按钮,模板在这里:
{% extends 'strona/Main.html' %}
{% block body %}
< form action="" method="post" enctype="multipart/form-data" >
{% csrf_token %}
{% for field in form %}
{{ field.label_tag }}
< /br >
{{ field }}
< /br >
< /br >
{% endfor %}
< button type="submit" class="btn btn-success">Submit</button >
< /form >
{% endblock %}
它一直说:&#34;无法分配&#34;&lt; SimpleLazyObject:&lt; django.contrib.auth.models.AnonymousUser对象位于0x038B5A70&gt;&gt;&#34;:&#34; SUser.user&#34;必须是&#34;用户&#34; 。实例&#34;
源代码:
VIEWS.PY
class RegPanelView(View):
panel = RegPanel
template_name = 'strona/RegistrationTemp.html'
registration = None
def get(self, request):
form = self.panel(None)
return render(request, self.template_name, {'form': form})
def post(self, request):
# form = self.panel(data=request.POST)
form = self.panel(request.POST)
if form.is_valid():
# user = form.save(commit=False)
# username = form.cleaned_data['username']
password1 = form.cleaned_data['password1']
password2 = form.cleaned_data['password2']
if password1 == password2:
newuser = form.save(commit=False)
newuser.user = request.user
newuser.save()
return redirect('strona:index')
return render(request, self.template_name, {'form': form})
MODELS.PY
class SUser(models.Model):
# User = user_model()
user = models.OneToOneField(User, on_delete=models.CASCADE)
username = models.CharField(max_length=200)
# last_login = models.DateTimeField(blank=True)
# is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
relations = models.ManyToManyField('self')
# is_anonymous = False
# is_authenticated = True
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ('', )
FORMS.PY
class RegPanel(UserCreationForm):
# password = forms.CharField(max_length=16, widget=forms.PasswordInput)
# first_name = forms.CharField(max_length=100, required=False)
# last_name = forms.CharField(max_length=100, required=False)
# is_staff = forms.BooleanField(required=False)
# birthday = forms.DateField(required=False)
class Meta:
model = SUser
fields = ['username', 'is_staff', 'first_name', 'last_name']
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
# user.set_password(self.cleaned_data["passwosrd1"])
if commit:
user.set_password(self.cleaned_data["password1"])
user.save()
# if commit:
# user.save()
return user
SETTINGS.PY:
AUTH_USER_MODEL = 'auth.User'
修改
完全错误跟踪:
文件 &#34; C:\用户\的Dawid \应用程序数据\本地\程序\的Python \ Python35-32 \ lib中\站点包\ django的-1.10.3-py3.5.egg \ django的\芯\处理程序\ exception.py& #34; 在内心 39. response = get_response(request)
文件 &#34; C:\用户\的Dawid \应用程序数据\本地\程序\的Python \ Python35-32 \ lib中\站点包\ django的-1.10.3-py3.5.egg \ django的\芯\处理程序\ base.py& #34; 在_get_response中 187. response = self.process_exception_by_middleware(e,request)
文件 &#34; C:\用户\的Dawid \应用程序数据\本地\程序\的Python \ Python35-32 \ lib中\站点包\ django的-1.10.3-py3.5.egg \ django的\芯\处理程序\ base.py& #34; 在_get_response中 185. response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件 &#34; C:\用户\的Dawid \应用程序数据\本地\程序\ Python的\ Python35-32 \ LIB \站点包\ Django的1.10.3-py3.5.egg \ Django的\意见\通用\ base.py& #34; 在视野中 68.返回self.dispatch(request,* args,** kwargs)
文件 &#34; C:\用户\的Dawid \应用程序数据\本地\程序\ Python的\ Python35-32 \ LIB \站点包\ Django的1.10.3-py3.5.egg \ Django的\意见\通用\ base.py& #34; 在发货 88.返回处理程序(request,* args,** kwargs)
文件&#34; C:\ Users \ Dawid \ PycharmProjects \ Zadanie \ strona \ views.py&#34;在邮寄 97. newuser.user = request.user
文件 &#34; C:\用户\的Dawid \应用程序数据\本地\程序\ Python的\ Python35-32 \ LIB \站点包\ Django的1.10.3-py3.5.egg \ Django的\ DB \型号\域\ related_descriptors的.py&#34; 在设置 211. self.field.remote_field.model._meta.object_name,
异常类型:ValueError at / index / register / Exception值:不能 分配&#34;&lt; SimpleLazyObject:&gt;&#34;:&#34; SUser.user&#34;必须是&#34;用户&#34;实例
请有人帮帮我吗? 亲切的问候。
答案 0 :(得分:0)
Django延迟加载request.user
,以便它可以是User
或AnonymousUser
,具体取决于身份验证状态。
if request.user.is_authenticated(): #then request.user will be `User` instance
newuser = form.save(commit=False)
newuser.user = request.user
newuser.save()