'User Registration with error: no such table: auth_user

I am trying to use Django's default Auth to handle register and log in.

setting.py:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'books',
)

MIDDLEWARE_CLASSES = (
    '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',
)

AUTH_USER_MODEL = 'books.User'

books.models.py:

class User(AbstractUser):
    account_balance = models.DecimalField(max_digits=5, decimal_places=2, default=0)

views.py:

from django.contrib.auth.forms import UserCreationForm

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            return HttpResponseRedirect("/accounts/profile/")
    else:
        form = UserCreationForm()
    return render(request, "registration/register.html", {'form': form,})

urls.py:

urlpatterns = patterns('',
    (r'^accounts/login/$', login),
    (r'^accounts/logout/$', logout),
    (r'^accounts/profile/$', profile),
    (r'^accounts/register/$', register),
)

I tried deleting the db.sqlite3 file and re-ran python manage.py syncdb but I still get this error message:

OperationalError at /accounts/register/
no such table: auth_user
Request Method: POST
Request URL:    http://127.0.0.1:8000/accounts/register/
Django Version: 1.7b4
Exception Type: OperationalError
Exception Value:    
no such table: auth_user


Solution 1:[1]

./manage.py migrate

If you've just enabled all the middlewares etc this will run each migration and add the missing tables.

Solution 2:[2]

Only thing you need to do is :

python manage.py migrate

and after that:

python manage.py createsuperuser

after that you can select username and password.

here is the sample output:

Username (leave blank to use 'hp'): admin
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.

Solution 3:[3]

This will work for django version <1.7:

Initialize the tables with the command

manage.py syncdb

This allows you to nominate a "super user" as well as initializing any tables.

Solution 4:[4]

it is need to make migration before create superuser.

python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
Username : admin
Password : 12345678

python manage.py runserver

Solution 5:[5]

Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. try running

python manage.py migrate

then run

python manage.py createsuperuser

Solution 6:[6]

If using a custom auth model, in your UserCreationForm subclass, you'll have to override both the metaclass and clean_username method as it references a hardcoded User class (the latter just until django 1.8).

class Meta(UserCreationForm.Meta):
        model = get_user_model()

    def clean_username(self):
        username = self.cleaned_data['username']

        try:
            self.Meta.model.objects.get(username=username)
        except self.Meta.model.DoesNotExist:
            return username

        raise forms.ValidationError(
            self.error_messages['duplicate_username'],
            code='duplicate_username',
        )

Solution 7:[7]

Before creating a custom user model, a first migration must be performed. Then install the application of your user model and add the AUTH_USER_MODEL.

As well:

class UserForm(UserCreationForm):

    class Meta:
        model = User
        fields = ("username",)

and

python manage.py migrate auth
python manage.py migrate

Solution 8:[8]

For custom forms( if you have made your own forms) use this command to migrate

python manage.py migrate --run-syncdb

Solution 9:[9]

On Django 1.11 I had to do this after following instructions in docs https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#substituting-a-custom-user-model

# create default database:
./manage.py migrate

# create my custom model migration:
# running `./manage.py makemigrations` was not enough
./manage.py makemigrations books
# specify one-off defaults

# create table with users:
./manage.py migrate

Solution 10:[10]

Just do the following flow

$ django-admin createproject <your project name>

under <your project dict> type django-admin createapp <app name>

under <app name>/admin.py

from django.contrib import admin
from .models import Post
admin.site.register(Post)

Go to the root project. Then $python manage.py migrate

Then it asks for username and password

Solution 11:[11]

Just perform migrations before registering the user.

Solution 12:[12]

theres four steps for adding a custom user model to django

  1. Create a CustomUser model
  2. update project/settings.py AUTH_USER_MODEL
  3. customize UserCreationForm & UserChangeForm
  4. add the custom user model to admin.py

you missed customize forms , add the CustomUser and CustomUserAdmin to admin.site.register() , then makemigrations nd migrate .

#proj_app/forms.py
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, UserChangeForm

class CustomUserCreationForm(UserCreationForm):
    class Meta:
            model = get_user_model()
            fields = ('email','username',)

class CustomUserChangeForm(UserChangeForm):
    class Meta:
            model = get_user_model()
            fields = ('email',  'username',)

#proj_app/admin.py
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin

from .forms import CustomUserCreationForm , CustomUserChangeForm

CustomUser = get_user_model()

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser
    list_display = ['email','username',]

admin.site.register(CustomUser, CustomUserAdmin)

here we extend the existing UserAdmin into CustomUserAdmin and tell django to use our new forms, custom user model, and list only the email and username of a user also we could add more of existing User fields to list_display

Solution 13:[13]

I have also faced the same problem "no such table: auth_user" when I was trying to deploy one of my Django website in a virtual environment.

Here is my solution which worked in my case:

In your settings.py file where you defined your database setting like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',

        'NAME': os.path.join(os.getcwd(), 'db.sqlite3'),
     }
 }  

just locate your db.sqlite3 database or any other database that you are using and write down a full path of your database , so the database setting will now look something like this ;

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/home/django/django_project/db.sqlite3',
    }
}  

I hope that your problem will resolve now.

Solution 14:[14]

python manage.py makemigrations then ? python manage.py migrate fixes it.

Assuming Apps defined/installed in settings.py exist in the project directory.

Solution 15:[15]

Please check how many python instances are running in background like in windows go--->task manager and check python instances and kill or end task i.e kill all python instances. run again using "py manage.py runserver" command. i hope it will be work fine....

Solution 16:[16]

If You did any changes in project/app then execute:

python manage.py migrate
python manage.py makemigrations
python manage.py createsuperuser

Solution 17:[17]

call these command

python manage.py makemigrations
python manage.py migrate