title: 'NoneType' object has no attribute 'set_password' in django registration and login
body: <p>I am entirely new to django.
  Trying to create login and registration system.</p>
  
  <p>User registers successfully, saved in database but I get this error after registering.</p>
  
  <p><strong>"'NoneType' object has no attribute 'set_password'"</strong></p>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>def register(request):
  if request.method == 'POST':
      form = SignUpForm(request.POST)
      if form.is_valid():
          user = form.save(commit=False)
          form.save()
          username = form.cleaned_data.get('username')
          fullname = form.cleaned_data.get('fullname')
          password = form.cleaned_data.get('password1')
          user = authenticate(request, username=username, password=password)
          messages.success(request, f'Welcome to blaza {fullname}')
          user.set_password(password)
          user.save()
          if user is not None:
              login(request, user)
          return redirect(reverse('home'))
  else:
      form = SignUpForm()
  return render(request, 'accounts/signup.html', {'form': form})
  </code></pre>
  
  <p>When I remove "user.set_password" it works but registered users can not login with their credentials even when the username and password is correct, It says incorrect username and password. (only admin account, superuser can login).</p>
  
  <p>So I researched and had to add the user.set_password and user = form.save (I get warning that local variable user value is not used)</p>
  
  <p><strong>forms.py</strong></p>
  
  <pre><code>class SignUpForm(UserCreationForm):
  username = forms.CharField(max_length=50)
  fullname = forms.CharField(max_length=200)
  email = forms.EmailField(max_length=200)
  password2 = None
  
  class Meta:
      model = User
      fields = ('username', 'fullname', 'email', 'password1')
  
  def clean_password1(self):
      password1 = self.cleaned_data.get('password1')
      try:
          password_validation.validate_password(password1, self.instance)
      except forms.ValidationError as error:
          self.add_error('password1', error)
          return password1
  </code></pre>
  
  <p><strong>Models.py</strong></p>
  
  <pre><code>class CustomUser(AbstractUser):
  class Meta:
      db_table = 'users'
  fullname = models.CharField(max_length=200)
  email = models.EmailField(max_length=150)
  profile_photo = models.ImageField(upload_to='images/profile_pics', default='images/nophoto.png')
  phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',
                               message="Phone number must be entered in the format: '+999999999'. Up to 15 "
                                       "digits "
                                       "allowed.")
  phone_number = models.CharField(validators=[phone_regex], max_length=13, default='')
  address = models.CharField(max_length=100, default='')
  has_store = models.BooleanField(default=False)
  </code></pre>
  
  <p><strong>signup.html</strong></p>
  
  <pre><code>&lt;form method="post"&gt;
  {% csrf_token %}
  {% for field in form %}
  &lt;p&gt;
    {{ field.label_tag }}&lt;br&gt;
    {{ field }}
    {% if field.help_text %}
      &lt;small style="color: grey"&gt;{{ field.help_text }}&lt;/small&gt;
    {% endif %}
    {% for error in field.errors %}
      &lt;p style="color: red"&gt;{{ error }}&lt;/p&gt;
    {% endfor %}
  &lt;/p&gt;
  {% endfor %}
  &lt;button type="submit"&gt;Sign up&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>urls.py</strong></p>
  
  <pre><code>urlpatterns = [
  path('admin/', admin.site.urls),
  path('', TemplateView.as_view(template_name='home.html'), name='home'),
  path('accounts/', include('users.urls')),
  path('accounts/', include('django.contrib.auth.urls')),
  ]
  </code></pre>
  
  <p>Or Someone help me with a secured and working registration and login that actually works.</p>