django Manager isn't available; 'auth.User' has been swapped for 'users.User'

Asked 2 years ago, Updated 2 years ago, 115 views

django is adding account creation functionality, but the error cannot be resolved.
You are using a custom user.
I am referring to the information on the Internet.
If you understand, please.

Internal Server Error: /accounts/signup/
Traceback (most recent call last):
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/core/handlers/exception.py", line 34, inner
    response=get_response(request)
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/core/handlers/base.py", line 115, in_get_response
    response=self.process_exception_by_middleware(e,request)
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/core/handlers/base.py", line 113, in_get_response
    response=wrapped_callback(request, *callback_args, **callback_kwargs)
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/views/generic/base.py", line 71, inview
    return self.dispatch (request, *args, **kwargs)
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
    return handler (request, *args, **kwargs)
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/views/generic/edit.py", line 172, inpost
    return super().post(request, *args, **kwargs)
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/views/generic/edit.py", line 141, in post
    if form.is_valid():
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/forms/forms.py", line 185, in is_valid
    return self.is_bound and not self.errors
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/forms/forms.py", line 180, in errors
    self.full_clean()
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/forms/forms.py", line383, in full_clean
    self._post_clean()
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/contrib/auth/forms.py", line 107, in_post_clean
    super()._post_clean()
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/forms/models.py", line 403, in_post_clean
    self.instance.full_clean(exclude=exclude, validate_unique=False)
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/db/models/base.py", line 1188, in full_clean
    self.clean()
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/contrib/auth/models.py", line 339, inclean
    self.email=self.__class__.objects.normalize_email(self.email)
  File"/anaconda3/envs/active/lib/python 3.7/site-packages/django/db/models/manager.py", line 188, in__get__
    cls._meta.swapped,
AttributeError: Manager isn't available; 'auth.User' has been swapped for 'users.User'
<form action="method="POST">
    {{ form.non_field_errors}}
    {% US>for field in form%}
    <div class="form-group">
        <label for="{{field.id_for_label}}}">{{field.label_tag}}</label>
        {{ field}}
        {{ field.errors}}
    </div>
    {% endfor%}
    {% csrf_token%}
    <button type="submit" class="btn btn-primary btn-lg">send</button>
</form>
 from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model

User=get_user_model()

classSignUpForm (UserCreationForm):
    """User Registration Form"""

    class Meta:
        model=User
        fields=('email',)

    def__init__(self, *args, **kwargs):
        super().__init__(*args,**kwargs)
        for field in self.fields.values():
            field.widget.attrs['class'] = 'form-control'

    def clean_email(self):
        email=self.cleaned_data ['email']
        User.objects.filter(email=email, is_active=False).delete()
        return email
class SignUp (CreateView):
    form_class = UserCreationForm
    template_name = "accounts/signup.html"
    success_url = reverse_lazy('')

    def form_valid(self, form):
        user=form.save(commit=False)
        user.is_active=False
        user.save()
        # Sending Activation URL
        current_site=get_current_site(self.request)
        # Get the domain
        domain=current_site.domain
        context = {
            'protocol':self.request.scheme,
            'domain': domain,
            'token': dumps (user.pk),
            'user': user,
        }
        # call template
        subject=render_to_string('mail/create/subject.txt', context)
        message=render_to_string('mail/create/message.txt', context)

        user.email_user(subject, message)
        return redirect('accounts:SignUpDone')

classSignUpDone (TemplateView):
    """User provisional registration"""
    template_name = 'accounts/signupdone.html'

Class SignUpComplete (TemplateView):
    """User registration after accessing URL in mail"""
    template_name = 'accounts/signupcomplete.html'
    timeout_seconds=getattr(settings, 'ACTIVATION_TIMEOUT_SECONDS', 60*60*24)# Default within 1 day

    default(self, request, **kwargs):
        "If ""token"" is correct, this registration."""
        token=kwargs.get('token')
        try:
            user_pk=loads(token, max_age=self.timeout_seconds)

        # Expired
        except SignatureExpired:
            return HttpResponseBadRequest()

        # Token is wrong
        except BadSignature:
            return HttpResponseBadRequest()

        # Token is fine.
        else:
            try:
                user=User.objects.get(pk=user_pk)
            except User.DoesNotExist:
                return HttpResponseBadRequest()
            else:
                if not user.is_active:
                    # If there is no problem, this registration will be accepted.
                    user.is_active=True
                    user.save()
                    return super().get(request,**kwargs)

        return HttpResponseBadRequest()

python django

2022-09-30 19:27

1 Answers

form_class=UserCreationForm

I changed the description to SignUpForm and it worked.


2022-09-30 19:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.