R
R
Rassul Nassyrov2020-09-16 14:57:05
Django
Rassul Nassyrov, 2020-09-16 14:57:05

Django user not being created?

Mistake:

for i in User.objects.all():
  File "/home/rassul/.local/lib/python3.8/site-packages/django/db/models/manager.py", line 187, in __get__
    raise AttributeError(
AttributeError: Manager isn't available; 'auth.User' has been swapped for 'user.User'


URLs.py:

path('user/createaccount', views.createaccount, name = 'createuser'),


models.py:
from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    years_old = models.CharField(max_length=2, null = True, blank = True)
    country = models.CharField(max_length=35, null = True, blank = True)
    city = models.CharField(max_length=35, null = True, blank = True)
    bio = models.CharField(max_length=150, null = True, blank = True)
    weburl = models.CharField(max_length=200, null = True, blank = True)
    webname = models.CharField(max_length=50, null = True, blank = True)
    image = models.ImageField(default = 'static/images/profile.webp', upload_to = "static/images/", blank = True)
    sex = models.CharField(max_length=10, blank = True)

    def __str__(self):
        return self.username


settings.py:
AUTH_USER_MODEL = 'user.User'
LOGIN_REDIRECT_URL = 'profile'
LOGOUT_REDIRECT_URL = 'profile'


Views.py:
def createaccount(request, *args, **kwargs):
    if request.method == "POST":
        for i in User.objects.all():
            if i.username == request.POST['login']:
                isloggnot = "True"
                return render(request, "fr/createaccount.html", {'isloggnot': isloggnot})
            elif i.email == request.POST['email']:
                isemailnot = "True"
                return render(request, "fr/createaccount.html", {'isemailnot': isemailnot})
            else:
                first_name = request.POST['name']
                last_name = request.POST['surname']
                username = request.POST['login']
                email = request.POST['email']
                password = request.POST['password']
                User.objects.create_user(password=password, username=username, email=email, is_active=True, is_superuser=False, first_name=first_name, last_name=lastname, date_joined=timezone.now())


createaccount.html:
<form method = 'POST'>
    {% csrf_token %}
    <div id = 'login'>
        <p style = 'text-align: center; margin-left: -37px;'>Регистрация</p>
        {% if isloggnot == "True" %}
        <p style = 'color:darkred; text-align: center; margin-left: -30px;'>Логин уже сушествует !</p>
        {% endif %}
        {% if isemailnot == "True" %}
        <p style = 'color:darkred; text-align: center; margin-left: -30px;'>Email уже сушетсвует!</p>
        {% endif %}
        {% if isadded == "True" %}
        <p onclick = "location.href = 'login'" style = 'color:rgb(60, 221, 11); text-align: center; margin-left: -30px;'>Успешно создан аккаунт! Войдите</p>
        {% endif %}
        <input placeholder="Придумайте логин" minlength="3" maxlength="30" name = 'login' type = 'text' style = 'width: 200px; height: 35px; margin-left: 130px;' required>
        <input placeholder="Ваше имя" name = 'name' minlength="3" maxlength="30" type = 'text' style = 'margin-top:10px; width: 200px; height: 35px; margin-left: 130px;' required>
        <input placeholder="Ваша фамилия" name = 'surname' minlength="3" maxlength="30" type = 'text' style = 'margin-top:10px; width: 200px; height: 35px; margin-left: 130px;' required>
        <input placeholder="Ваш email"  name = 'email' minlength="3" maxlength="80" type = 'email' style = 'margin-top:10px; width: 200px; height: 35px; margin-left: 130px;' required>
        <br>
        <select name = 'sex' style = 'margin-left: 130px; margin-top: 10px; width: 220px;'>
            <option name = 'sex'>None</option>
            <option name = 'sex'>мужской</option>
            <option name = 'sex'>женский</option>
        </select>
        <br>
        <br>
        <input placeholder="Ваш пороль" minlength="5" maxlength="80" name = 'password' type = 'text' style = 'width: 200px; height: 35px; margin-left: 130px; margin-top: 10px;' required>
        <button type = 'submit' style = 'background-color: rgb(26, 66, 18); color: red; width: 220px; height: 35px; margin-left: 130px; margin-top: 10px;' required>Cоздать</button>
        <br>
        <br>
        <a href = "login" style = 'color: black; margin-top:10px; text-align: center; margin-left: 160px;'>Уменя есть аккаунт</a>
        <br>
        <br>
        <a href = "login" style = 'color: black; margin-top:10px; text-align: center; margin-left: 193px;'>Правила НС</a>
    </div>
    </form>


If that application is called user.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2020-09-16
@nssr02

In views.py , you
probably need to import the user like this:

from django.contrib.auth import get_user_model
User = get_user_model()

D
Dr. Bacon, 2020-09-16
@bacon

Well, in the error it clearly says that Manager is needed https://docs.djangoproject.com/en/3.1/topics/auth/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question