TypeError when creating a new user

317
February 16, 2018, at 11:10 PM

After following this tutorial to create a signup view, I have encountered an error.

Here is the view:

def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            new_user = form.save(commit=False)
            new_user.is_active = False
            new_user.save()
            current_site = get_current_site(request)
            subject = "Activate your DC-Elects account"
            message = render_to_string('registration/account_activation_email.html', {
                'user': new_user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(used.id)),
                'token': account_activation_token.make_token(user),
            })
            new_user.email_user(subject, message)
            return reverse('elections:account_activation_sent')
    else:
        form = SignUpForm()
    return render(request, 'registration/signup.html',{
        'form': form,
    })

Here is the extended user model:

class Profile(models.Model):
    UserID = models.OneToOneField(User, on_delete=models.CASCADE)
    GENDERS = (("M","Male"), ("F","Female"))
    Gender = models.CharField(max_length=1, choices=GENDERS)
    UserTypeID = models.ForeignKey(UserType, on_delete=models.PROTECT, blank=True, null=True)
    ProfilePicture = models.ImageField(default='default_profile.png', upload_to="imageposts/")
    EmailConfirmed = models.BooleanField(default=False)
    # For student number, can you have choices - Teacher or input()?
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

Everything goes fine when filling out the form as the user, but when I submit the form, I get this error:

Traceback (most recent call last):
  File "C:\python\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "C:\python\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\python\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\cbuch1800\Coursework\mysite\elections\views.py", line 325, in signup
    new_user.save()
  File "C:\python\lib\site-packages\django\contrib\auth\base_user.py", line 80, in save
    super(AbstractBaseUser, self).save(*args, **kwargs)
  File "C:\python\lib\site-packages\django\db\models\base.py", line 808, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\python\lib\site-packages\django\db\models\base.py", line 848, in save_base
    update_fields=update_fields, raw=raw, using=using,
  File "C:\python\lib\site-packages\django\dispatch\dispatcher.py", line 193, in send
    for receiver in self._live_receivers(sender)
  File "C:\python\lib\site-packages\django\dispatch\dispatcher.py", line 193, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "C:\Users\cbuch1800\Coursework\mysite\elections\models.py", line 25, in create_user_profile
    Profile.objects.create(user=instance)
  File "C:\python\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\python\lib\site-packages\django\db\models\query.py", line 392, in create
    obj = self.model(**kwargs)
  File "C:\python\lib\site-packages\django\db\models\base.py", line 573, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'user' is an invalid keyword argument for this function

I'm not sure where or why the error is being raised, since a new user is still being created and saved to the database, I am just getting this error instead of the email being sent.

How can I prevent this error?

Answer 1

You have field UserID in Profile model, so you need to use it in create method instead of user:

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(UserID=instance)
Answer 2
Profile.objects.create(user=instance)

Your Profile model definition does not contain any field named user.

Rent Charter Buses Company
READ ALSO
Members role command

Members role command

so i been trying to get this command $100ivShiny to give my members the actual rank 100ivShinybut i cant seem to find the solution

226
Dask read_csv: skip periodically ocurring lines

Dask read_csv: skip periodically ocurring lines

I want to use Dask to read in a large file of atom coordinates at multiple time stepsThe format is called XYZ file, and it looks like this:

239
HSTACK CNN output with Lambda function

HSTACK CNN output with Lambda function

I have made one type of NN that uses CNN and LSTM :

245
How to pass two estimator objects to sklearn&#39;s GridSearchCV so that they have the same parameters in each step?

How to pass two estimator objects to sklearn's GridSearchCV so that they have the same parameters in each step?

I'm trying to use GridSearchCV from SKlearn to tune hyperparameters for my estimator

196