0

I'm trying to create a registration page. I'm getting this error.

The views.py:

def login_view(request):
    if request.method == "POST":

        # Attempt to sign user in
        username = request.POST["username"]
        password = request.POST["password"]
        user = authenticate(request, username=username, password=password)

        # Check if authentication successful
        if user is not None:
            login(request, user)
            return HttpResponseRedirect(reverse("index"))
        else:
            return render(request, "auctions/login.html", {
                "message": "Invalid username and/or password."
            })
    else:
        return render(request, "auctions/login.html")


def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse("index"))


def register(request):
    if request.method == "POST":
        username = request.POST["username"]
        email = request.POST["email"]

        # Ensure password matches confirmation
        password = request.POST["password"]
        confirmation = request.POST["confirmation"]
        if password != confirmation:
            return render(request, "auctions/register.html", {
                "message": "Passwords must match."
            })

        # Attempt to create new user
        try:
            print("momooo!")
            user = User.objects.create_user(username, email, password)
            print(("more mooomoo"))
            user.save()
        except IntegrityError:
            return render(request, "auctions/register.html", {
                "message": "Username already taken."
            })
        login(request, user)
        return HttpResponseRedirect(reverse("index"))
    else:
        return render(request, "auctions/register.html")

The register.html page:

{% extends "auctions/layout.html" %}

{% block body %}

    <h2>Register</h2>

    {% if message %}
        <div>{{ message }}</div>
    {% endif %}

    <form action="{% url 'register' %}" method="post">
        {% csrf_token %}
        <div class="form-group">
            <input class="form-control" autofocus type="text" name="username" placeholder="Username">
        </div>
        <div class="form-group">
            <input class="form-control" type="email" name="email" placeholder="Email Address">
        </div>
        <div class="form-group">
            <input class="form-control" type="password" name="password" placeholder="Password">
        </div>
        <div class="form-group">
            <input class="form-control" type="password" name="confirmation" placeholder="Confirm Password">
        </div>
        <input class="btn btn-primary" type="submit" value="Register">
    </form>

    Already have an account? <a href="{% url 'login' %}">Log In here.</a>

{% endblock %}

Inside models.py:

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


class User(AbstractUser):
    # pass
    def create_user(username, email, password):
        print("hello!!!!")


I am getting this error when I try to register a new user:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/register

Django Version: 3.2.4
Python Version: 3.9.5
Installed Applications:
['auctions',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)

The above exception (no such table: auctions_user) was the direct cause of the following exception:
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Kaij\Documents\coding\cs50\commerce\auctions\views.py", line 55, in register
    user = User.objects.create_user(username, email, password)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\models.py", line 152, in create_user
    return self._create_user(username, email, password, **extra_fields)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\models.py", line 146, in _create_user
    user.save(using=self._db)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\base_user.py", line 67, in save
    super().save(*args, **kwargs)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 726, in save
    self.save_base(using=using, force_insert=force_insert,
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 763, in save_base
    updated = self._save_table(
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 868, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
    return manager._insert(
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 1270, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1416, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /register
Exception Value: no such table: auctions_user

I'm confused about ther error 'no such table: auctions_user'. I have not created that table so not sure why they need it or where it's referenced.

halfer
  • 19,824
  • 17
  • 99
  • 186
Katie Melosto
  • 1,047
  • 2
  • 14
  • 35

2 Answers2

2

I found what worked was to run

python manage.py makemigrations YOURAPPNAME

specific for my app and then I also had to

comment out:

INSTALLED_APPS = [
...
#'django.contrib.admin',
...
]

And also comment out:

# path('admin/', admin.site.urls) in urls.py

Then finally I could run

python manage.py migrate

Then add them back [removing the ## to remove the comment]

After that the table error was gone.

These previous posts helped:

  1. django-OperationalError at /register/ no such table: auth_user
  2. django.db.migrations.exceptions.InconsistentMigrationHistory
Katie Melosto
  • 1,047
  • 2
  • 14
  • 35
1

Run python manage.py makemigration then python manage.py migrate to create the user table

Mohamed ElKalioby
  • 1,908
  • 1
  • 12
  • 13