django: User Registration with error: no such table: auth_user

拥有回忆 提交于 2019-11-28 19:12:47

Update

You are probably getting this error because you are using UserCreationForm modelform, in which in META it contains User(django.contrib.auth.models > User) as model.

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

And here you are using your own custom auth model, so tables related to User has not been created. So here you have to use your own custom modelform. where in Meta class, model should be your User(books.User) model

./manage.py migrate

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

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: xyz@gmail.com
Password:
Password (again):
Superuser created successfully.
holdenweb

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.

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',
        )

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.

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

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

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....

Karthick Sakkaravarthi

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

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

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
Suraj Verma

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

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

call these command

python manage.py makemigrations
python manage.py migrate
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!