I have a problem with adding new custom user to the database. I know thare's some threads about it on the internet but I couldn't find solution for my problem. After hitting "Submit" button in registration page which template is here:
{% 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 %}
it keeps saying that: "Cannot assign "< SimpleLazyObject: < django.contrib.auth.models.AnonymousUser object at 0x038B5A70 >>": "SUser.user" must be a "User" instance."
Source Codes:
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'
EDIT
FULL ERROR TRACEBACK:
File "C:\Users\Dawid\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-1.10.3-py3.5.egg\django\core\handlers\exception.py" in inner 39. response = get_response(request)
File "C:\Users\Dawid\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-1.10.3-py3.5.egg\django\core\handlers\base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Dawid\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-1.10.3-py3.5.egg\django\core\handlers\base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Dawid\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-1.10.3-py3.5.egg\django\views\generic\base.py" in view 68. return self.dispatch(request, *args, **kwargs)
File "C:\Users\Dawid\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-1.10.3-py3.5.egg\django\views\generic\base.py" in dispatch 88. return handler(request, *args, **kwargs)
File "C:\Users\Dawid\PycharmProjects\Zadanie\strona\views.py" in post 97. newuser.user = request.user
File "C:\Users\Dawid\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-1.10.3-py3.5.egg\django\db\models\fields\related_descriptors.py" in set 211. self.field.remote_field.model._meta.object_name,
Exception Type: ValueError at /index/register/ Exception Value: Cannot assign "< SimpleLazyObject: >": "SUser.user" must be a "User" instance.
Please could someone help me out? Kind regards.